@bunny-agent/daemon 0.9.29-beta.0 → 0.9.29-beta.10

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/nextjs.js CHANGED
@@ -116838,7 +116838,7 @@ var require_core3 = __commonJS({
116838
116838
  return match2 && match2.index === 0;
116839
116839
  }
116840
116840
  var BACKREF_RE = /\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./;
116841
- function join38(regexps, separator = "|") {
116841
+ function join39(regexps, separator = "|") {
116842
116842
  let numCaptures = 0;
116843
116843
  return regexps.map((regex2) => {
116844
116844
  numCaptures += 1;
@@ -117142,7 +117142,7 @@ var require_core3 = __commonJS({
117142
117142
  this.exec = () => null;
117143
117143
  }
117144
117144
  const terminators = this.regexes.map((el) => el[1]);
117145
- this.matcherRe = langRe(join38(terminators), true);
117145
+ this.matcherRe = langRe(join39(terminators), true);
117146
117146
  this.lastIndex = 0;
117147
117147
  }
117148
117148
  /** @param {string} s */
@@ -168060,14 +168060,14 @@ var require_graceful_fs = __commonJS({
168060
168060
  return close;
168061
168061
  })(fs16.close);
168062
168062
  fs16.closeSync = (function(fs$closeSync) {
168063
- function closeSync2(fd) {
168063
+ function closeSync3(fd) {
168064
168064
  fs$closeSync.apply(fs16, arguments);
168065
168065
  resetQueue();
168066
168066
  }
168067
- Object.defineProperty(closeSync2, previousSymbol, {
168067
+ Object.defineProperty(closeSync3, previousSymbol, {
168068
168068
  value: fs$closeSync
168069
168069
  });
168070
- return closeSync2;
168070
+ return closeSync3;
168071
168071
  })(fs16.closeSync);
168072
168072
  if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || "")) {
168073
168073
  process.on("exit", function() {
@@ -206492,10 +206492,15 @@ async function fsStat(state, q2) {
206492
206492
  const root = resolveVolumeRoot(state, q2.volume);
206493
206493
  const target = resolveUnderRoot(root, q2.path);
206494
206494
  const stat6 = await fs2.stat(target);
206495
+ const created_at = msToIsoOrNull(stat6.birthtimeMs) ?? msToIsoOrNull(stat6.ctimeMs);
206495
206496
  return ok({
206496
206497
  path: target,
206497
206498
  is_dir: stat6.isDirectory(),
206498
- size: stat6.isFile() ? stat6.size : 0
206499
+ size: stat6.isFile() ? stat6.size : 0,
206500
+ created_at,
206501
+ modified_at: msToIsoOrNull(
206502
+ stat6.mtimeMs
206503
+ )
206499
206504
  });
206500
206505
  }
206501
206506
  async function fsExists(state, q2) {
@@ -208375,7 +208380,7 @@ function createOpenCodeRunner(options2 = {}) {
208375
208380
 
208376
208381
  // ../../packages/runner-pi/dist/pi-runner.js
208377
208382
  import { appendFileSync as appendFileSync5, existsSync as existsSync28, unlinkSync as unlinkSync6 } from "node:fs";
208378
- import { join as join35 } from "node:path";
208383
+ import { join as join36 } from "node:path";
208379
208384
 
208380
208385
  // ../../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
208381
208386
  var dist_exports = {};
@@ -263361,8 +263366,8 @@ var generateImageSchema = {
263361
263366
  },
263362
263367
  quality: {
263363
263368
  type: "string",
263364
- enum: ["standard", "hd"],
263365
- description: "Image quality (OpenAI only). Defaults to standard."
263369
+ enum: ["low", "medium", "high", "auto"],
263370
+ description: "Image quality. Defaults to auto."
263366
263371
  }
263367
263372
  },
263368
263373
  required: ["prompt"],
@@ -263520,11 +263525,11 @@ function buildImageGenerateTool(cwd, imageModelId, baseUrl, apiKey) {
263520
263525
  ],
263521
263526
  // biome-ignore lint/suspicious/noExplicitAny: plain JSON Schema compatible with TypeBox TSchema
263522
263527
  parameters: generateImageSchema,
263523
- async execute(_toolCallId, params, _signal, _onUpdate) {
263528
+ async execute(_toolCallId, params, signal, _onUpdate) {
263524
263529
  const p = params;
263525
263530
  const prompt = p.prompt;
263526
263531
  const size = p.size ?? "1024x1024";
263527
- const quality = p.quality ?? "standard";
263532
+ const quality = p.quality ?? "auto";
263528
263533
  const rawFilename = p.filename;
263529
263534
  const filename = rawFilename ? extname2(rawFilename) ? rawFilename : `${rawFilename}.png` : `image_${Date.now()}.png`;
263530
263535
  const filePath = join34(cwd, filename.replace(/[^a-zA-Z0-9_\-./]/g, "_"));
@@ -263544,7 +263549,8 @@ function buildImageGenerateTool(cwd, imageModelId, baseUrl, apiKey) {
263544
263549
  quality,
263545
263550
  response_format: "b64_json",
263546
263551
  output_format: "png"
263547
- })
263552
+ }),
263553
+ signal
263548
263554
  });
263549
263555
  if (!res.ok) {
263550
263556
  throw new Error(`Image generation failed (${res.status}): ${await res.text()}`);
@@ -263561,6 +263567,7 @@ function buildImageGenerateTool(cwd, imageModelId, baseUrl, apiKey) {
263561
263567
  ],
263562
263568
  details: {
263563
263569
  filePath: savedPath,
263570
+ ...json.usage != null ? { usage: { raw: { [imageModelId]: json.usage } } } : {},
263564
263571
  response: json
263565
263572
  }
263566
263573
  };
@@ -263649,7 +263656,7 @@ function buildImageEditTool(cwd, imageModelId, baseUrl, apiKey) {
263649
263656
  ],
263650
263657
  // biome-ignore lint/suspicious/noExplicitAny: plain JSON Schema compatible with TypeBox TSchema
263651
263658
  parameters: editImageSchema,
263652
- async execute(_toolCallId, params, _signal, _onUpdate) {
263659
+ async execute(_toolCallId, params, signal, _onUpdate) {
263653
263660
  const { readFileSync: readFileSync24, existsSync: existsSync31 } = await import("node:fs");
263654
263661
  const { resolve: resolve14, basename: basename12 } = await import("node:path");
263655
263662
  const p = params;
@@ -263717,7 +263724,8 @@ function buildImageEditTool(cwd, imageModelId, baseUrl, apiKey) {
263717
263724
  "Content-Type": type,
263718
263725
  Authorization: `Bearer ${apiKey}`
263719
263726
  },
263720
- body
263727
+ body,
263728
+ signal
263721
263729
  });
263722
263730
  if (!res.ok) {
263723
263731
  throw new Error(`Image edit failed (${res.status}): ${await res.text()}`);
@@ -263747,6 +263755,7 @@ function buildImageEditTool(cwd, imageModelId, baseUrl, apiKey) {
263747
263755
  ],
263748
263756
  details: {
263749
263757
  filePath: savedPath,
263758
+ ...json.usage != null ? { usage: { raw: { [imageModelId]: json.usage } } } : {},
263750
263759
  response: json
263751
263760
  }
263752
263761
  };
@@ -263763,12 +263772,315 @@ function buildImageEditTool(cwd, imageModelId, baseUrl, apiKey) {
263763
263772
  };
263764
263773
  }
263765
263774
 
263775
+ // ../../packages/runner-pi/dist/session-utils.js
263776
+ import { closeSync as closeSync2, fstatSync, openSync as openSync2, readdirSync as readdirSync12, readSync as readSync2, statSync as statSync13 } from "node:fs";
263777
+ import { join as join35 } from "node:path";
263778
+ var MAX_SESSION_FILE_BYTES = Number(process.env.SANDAGENT_MAX_SESSION_BYTES) || 10 * 1024 * 1024;
263779
+ function resolveSessionPathById(cwd, sessionId) {
263780
+ const tempMgr = SessionManager.create(cwd);
263781
+ const sessionsDir = tempMgr.getSessionDir();
263782
+ try {
263783
+ const suffix = `_${sessionId}.jsonl`;
263784
+ const match2 = readdirSync12(sessionsDir).find((f3) => f3.endsWith(suffix));
263785
+ return match2 ? join35(sessionsDir, match2) : void 0;
263786
+ } catch {
263787
+ return void 0;
263788
+ }
263789
+ }
263790
+ function isSessionFileTooLarge(sessionPath2) {
263791
+ try {
263792
+ return statSync13(sessionPath2).size > MAX_SESSION_FILE_BYTES;
263793
+ } catch {
263794
+ return false;
263795
+ }
263796
+ }
263797
+ function readTailEntries(sessionPath2, tailBytes = 1024 * 1024) {
263798
+ let fd;
263799
+ try {
263800
+ fd = openSync2(sessionPath2, "r");
263801
+ } catch {
263802
+ return [];
263803
+ }
263804
+ try {
263805
+ const fileSize = fstatSync(fd).size;
263806
+ const readStart = Math.max(0, fileSize - tailBytes);
263807
+ const readLen = fileSize - readStart;
263808
+ const buf = Buffer.alloc(readLen);
263809
+ readSync2(fd, buf, 0, readLen, readStart);
263810
+ const tail = buf.toString("utf8");
263811
+ const entries = [];
263812
+ for (const line of tail.split("\n")) {
263813
+ const trimmed = line.trim();
263814
+ if (!trimmed)
263815
+ continue;
263816
+ try {
263817
+ entries.push(JSON.parse(trimmed));
263818
+ } catch {
263819
+ }
263820
+ }
263821
+ return entries;
263822
+ } finally {
263823
+ closeSync2(fd);
263824
+ }
263825
+ }
263826
+ function extractSessionContext(sessionPath2) {
263827
+ const entries = readTailEntries(sessionPath2);
263828
+ if (entries.length === 0)
263829
+ return void 0;
263830
+ for (let i2 = entries.length - 1; i2 >= 0; i2--) {
263831
+ const e2 = entries[i2];
263832
+ if (e2.type === "compaction" && typeof e2.summary === "string") {
263833
+ return e2.summary;
263834
+ }
263835
+ }
263836
+ const recentMessages = [];
263837
+ const MAX_MESSAGES = 6;
263838
+ for (let i2 = entries.length - 1; i2 >= 0 && recentMessages.length < MAX_MESSAGES; i2--) {
263839
+ const e2 = entries[i2];
263840
+ if (e2.type !== "message")
263841
+ continue;
263842
+ const msg = e2.message;
263843
+ if (!msg)
263844
+ continue;
263845
+ if (msg.role !== "user" && msg.role !== "assistant")
263846
+ continue;
263847
+ let text = "";
263848
+ if (typeof msg.content === "string") {
263849
+ text = msg.content;
263850
+ } else if (Array.isArray(msg.content)) {
263851
+ text = msg.content.filter((c) => c.type === "text" && c.text).map((c) => c.text).join("\n");
263852
+ }
263853
+ if (text) {
263854
+ recentMessages.unshift(`[${msg.role}]: ${text}`);
263855
+ }
263856
+ }
263857
+ if (recentMessages.length === 0)
263858
+ return void 0;
263859
+ return "## Previous Session Context (auto-extracted)\n\nThe following is the tail of the previous conversation:\n\n" + recentMessages.join("\n\n");
263860
+ }
263861
+
263862
+ // ../../packages/runner-pi/dist/usage-metadata.js
263863
+ function usageToMessageMetadata(usage) {
263864
+ return {
263865
+ input_tokens: usage.input,
263866
+ output_tokens: usage.output,
263867
+ cache_read_input_tokens: usage.cacheRead,
263868
+ cache_creation_input_tokens: usage.cacheWrite
263869
+ };
263870
+ }
263871
+ function accumulateToolUsage(tally, raw) {
263872
+ for (const [key, row] of Object.entries(raw)) {
263873
+ const existing = tally[key];
263874
+ if (existing) {
263875
+ for (const [field, val] of Object.entries(row)) {
263876
+ if (typeof val === "number")
263877
+ existing[field] = (existing[field] ?? 0) + val;
263878
+ }
263879
+ } else {
263880
+ const nums = {};
263881
+ for (const [field, val] of Object.entries(row)) {
263882
+ if (typeof val === "number")
263883
+ nums[field] = val;
263884
+ }
263885
+ tally[key] = nums;
263886
+ }
263887
+ }
263888
+ }
263889
+ function getUsageFromAgentEndMessages(messages) {
263890
+ for (let i2 = messages.length - 1; i2 >= 0; i2--) {
263891
+ const m2 = messages[i2];
263892
+ if (m2.role === "assistant" && m2.usage != null)
263893
+ return m2.usage;
263894
+ }
263895
+ return void 0;
263896
+ }
263897
+
263898
+ // ../../packages/runner-pi/dist/stream-converter.js
263899
+ function emitStreamError(errorText) {
263900
+ const errorLine = "data: " + JSON.stringify({ type: "error", errorText }) + "\n\n";
263901
+ const finishLine = "data: " + JSON.stringify({ type: "finish", finishReason: "error" }) + "\n\n";
263902
+ return [errorLine, finishLine, "data: [DONE]\n\n"];
263903
+ }
263904
+ function extractToolResultText(result) {
263905
+ if (result !== null && typeof result === "object") {
263906
+ const r2 = result;
263907
+ if (Array.isArray(r2.content) && r2.content.length > 0) {
263908
+ const text = r2.content.filter((c) => c.type === "text" && typeof c.text === "string").map((c) => c.text).join("\n");
263909
+ if (text.length > 0)
263910
+ return text;
263911
+ }
263912
+ }
263913
+ if (typeof result === "string")
263914
+ return result;
263915
+ try {
263916
+ return JSON.stringify(result);
263917
+ } catch {
263918
+ return String(result);
263919
+ }
263920
+ }
263921
+ function sseData(obj) {
263922
+ return "data: " + JSON.stringify(obj) + "\n\n";
263923
+ }
263924
+ var PiAISDKStreamConverter = class {
263925
+ constructor(options2) {
263926
+ this.options = options2;
263927
+ this.messageId = "msg_" + Date.now() + "_" + Math.random().toString(36).slice(2);
263928
+ this.toolUsageTally = {};
263929
+ this.activeTextPartId = null;
263930
+ this.hasStarted = false;
263931
+ this.hasFinished = false;
263932
+ }
263933
+ get finished() {
263934
+ return this.hasFinished;
263935
+ }
263936
+ forceError(errorText) {
263937
+ if (this.hasFinished)
263938
+ return [];
263939
+ return [...this.ensureStart(), ...this.finishError(errorText)];
263940
+ }
263941
+ handleEvent(event, aborted) {
263942
+ if (this.hasFinished)
263943
+ return [];
263944
+ const chunks = [...this.ensureStart()];
263945
+ if (event.type === "message_start") {
263946
+ const msg = event.message;
263947
+ if (msg?.role === "assistant")
263948
+ chunks.push(...this.endTextStreamIfOpen());
263949
+ return chunks;
263950
+ }
263951
+ if (event.type === "message_end")
263952
+ return chunks;
263953
+ if (event.type === "message_update") {
263954
+ const sub = event.assistantMessageEvent;
263955
+ if (sub.type === "text_start")
263956
+ chunks.push(...this.endTextStreamIfOpen(), ...this.openTextStream());
263957
+ else if (sub.type === "text_delta")
263958
+ chunks.push(...this.emitTextDelta(sub.delta));
263959
+ else if (sub.type === "toolcall_start")
263960
+ chunks.push(...this.endTextStreamIfOpen());
263961
+ return chunks;
263962
+ }
263963
+ if (event.type === "tool_execution_start") {
263964
+ chunks.push(...this.endTextStreamIfOpen());
263965
+ chunks.push(sseData({
263966
+ type: "tool-input-start",
263967
+ toolCallId: event.toolCallId,
263968
+ toolName: event.toolName,
263969
+ dynamic: true,
263970
+ providerExecuted: true
263971
+ }), sseData({
263972
+ type: "tool-input-available",
263973
+ toolCallId: event.toolCallId,
263974
+ toolName: event.toolName,
263975
+ input: event.args,
263976
+ dynamic: true,
263977
+ providerExecuted: true
263978
+ }));
263979
+ return chunks;
263980
+ }
263981
+ if (event.type === "tool_execution_end") {
263982
+ const output = this.options.redactText(this.options.normalizeToolOutput(event.result));
263983
+ const raw = event.result?.details?.usage?.raw;
263984
+ if (raw != null)
263985
+ accumulateToolUsage(this.toolUsageTally, raw);
263986
+ chunks.push(sseData({
263987
+ type: "tool-output-available",
263988
+ toolCallId: event.toolCallId,
263989
+ output,
263990
+ isError: event.isError,
263991
+ dynamic: true,
263992
+ providerExecuted: true
263993
+ }));
263994
+ return chunks;
263995
+ }
263996
+ if (event.type === "agent_end") {
263997
+ if (aborted) {
263998
+ chunks.push(...this.finishError("Run aborted by signal."));
263999
+ } else {
264000
+ const errorMsg = this.options.getErrorFromAgentEndMessages(event.messages);
264001
+ if (errorMsg)
264002
+ chunks.push(...this.finishError(errorMsg));
264003
+ else
264004
+ chunks.push(...this.finishSuccess(this.options.getUsageFromAgentEndMessages(event.messages)));
264005
+ }
264006
+ return chunks;
264007
+ }
264008
+ return chunks;
264009
+ }
264010
+ ensureStart() {
264011
+ if (this.hasStarted)
264012
+ return [];
264013
+ this.hasStarted = true;
264014
+ return [
264015
+ sseData({ type: "start", messageId: this.messageId }),
264016
+ sseData({
264017
+ type: "message-metadata",
264018
+ messageMetadata: { sessionId: this.options.sessionId }
264019
+ })
264020
+ ];
264021
+ }
264022
+ newTextPartId() {
264023
+ return "text_" + Date.now() + "_" + Math.random().toString(36).slice(2) + "_" + Math.random().toString(36).slice(2);
264024
+ }
264025
+ openTextStream() {
264026
+ this.activeTextPartId = this.newTextPartId();
264027
+ return [sseData({ type: "text-start", id: this.activeTextPartId })];
264028
+ }
264029
+ emitTextDelta(rawDelta) {
264030
+ const delta = rawDelta ? this.options.redactText(rawDelta) : void 0;
264031
+ if (!delta)
264032
+ return [];
264033
+ const startChunk = this.activeTextPartId == null ? this.openTextStream() : [];
264034
+ return [
264035
+ ...startChunk,
264036
+ sseData({ type: "text-delta", id: this.activeTextPartId, delta })
264037
+ ];
264038
+ }
264039
+ endTextStreamIfOpen() {
264040
+ if (this.activeTextPartId == null)
264041
+ return [];
264042
+ const id = this.activeTextPartId;
264043
+ this.activeTextPartId = null;
264044
+ return [sseData({ type: "text-end", id })];
264045
+ }
264046
+ finishSuccess(usage) {
264047
+ const chunks = [...this.endTextStreamIfOpen()];
264048
+ const raw = {};
264049
+ let chatUsage;
264050
+ if (usage) {
264051
+ const { id } = this.options.model;
264052
+ chatUsage = {
264053
+ type: "chat",
264054
+ ...usageToMessageMetadata(usage)
264055
+ };
264056
+ raw[id] = chatUsage;
264057
+ }
264058
+ for (const [key, tally] of Object.entries(this.toolUsageTally)) {
264059
+ raw[key] = { ...tally };
264060
+ }
264061
+ const finishPayload = {
264062
+ type: "finish",
264063
+ finishReason: "stop"
264064
+ };
264065
+ if (usage) {
264066
+ finishPayload.messageMetadata = { usage: { ...chatUsage, raw } };
264067
+ }
264068
+ chunks.push(sseData(finishPayload), "data: [DONE]\n\n");
264069
+ this.hasFinished = true;
264070
+ return chunks;
264071
+ }
264072
+ finishError(errorText) {
264073
+ this.hasFinished = true;
264074
+ return emitStreamError(errorText);
264075
+ }
264076
+ };
264077
+
263766
264078
  // ../../packages/runner-pi/dist/web-tools.js
263767
264079
  var braveProvider = {
263768
264080
  id: "brave",
263769
264081
  label: "Brave Search",
263770
264082
  envKeys: ["BRAVE_API_KEY"],
263771
- async search({ apiKey, query, count, country, freshness }) {
264083
+ async search({ apiKey, query, count, country, freshness, signal }) {
263772
264084
  const params = new URLSearchParams({
263773
264085
  q: query,
263774
264086
  count: String(Math.min(count, 20))
@@ -263782,7 +264094,8 @@ var braveProvider = {
263782
264094
  Accept: "application/json",
263783
264095
  "Accept-Encoding": "gzip",
263784
264096
  "X-Subscription-Token": apiKey
263785
- }
264097
+ },
264098
+ signal
263786
264099
  });
263787
264100
  if (!res.ok) {
263788
264101
  const body = await res.text().catch(() => "");
@@ -263803,14 +264116,14 @@ ${body}`);
263803
264116
  });
263804
264117
  }
263805
264118
  }
263806
- return results;
264119
+ return { results };
263807
264120
  }
263808
264121
  };
263809
264122
  var tavilyProvider = {
263810
264123
  id: "tavily",
263811
264124
  label: "Tavily",
263812
264125
  envKeys: ["TAVILY_API_KEY"],
263813
- async search({ apiKey, query, count }) {
264126
+ async search({ apiKey, query, count, signal }) {
263814
264127
  const res = await fetch("https://api.tavily.com/search", {
263815
264128
  method: "POST",
263816
264129
  headers: { "Content-Type": "application/json" },
@@ -263819,7 +264132,8 @@ var tavilyProvider = {
263819
264132
  query,
263820
264133
  max_results: Math.min(count, 10),
263821
264134
  include_answer: false
263822
- })
264135
+ }),
264136
+ signal
263823
264137
  });
263824
264138
  if (!res.ok) {
263825
264139
  const body = await res.text().catch(() => "");
@@ -263837,7 +264151,7 @@ ${body}`);
263837
264151
  });
263838
264152
  }
263839
264153
  }
263840
- return results;
264154
+ return { results };
263841
264155
  }
263842
264156
  };
263843
264157
  var AUTO_DETECT_ORDER = [braveProvider, tavilyProvider];
@@ -263872,9 +264186,12 @@ var BROWSER_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/53
263872
264186
  function htmlToText(html2) {
263873
264187
  return html2.replace(/<(script|style|noscript)[^>]*>[\s\S]*?<\/\1>/gi, "").replace(/<br\s*\/?>/gi, "\n").replace(/<\/(p|div|h[1-6]|li|tr)>/gi, "\n").replace(/<(p|div|h[1-6]|li|tr)[^>]*>/gi, "\n").replace(/<[^>]+>/g, "").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/g, " ").replace(/[ \t]+/g, " ").replace(/\n{3,}/g, "\n\n").trim();
263874
264188
  }
263875
- async function fetchPageContent(url) {
264189
+ async function fetchPageContent(url, externalSignal) {
263876
264190
  const controller = new AbortController();
263877
264191
  const timeout = setTimeout(() => controller.abort(), 15e3);
264192
+ externalSignal?.addEventListener("abort", () => controller.abort(), {
264193
+ once: true
264194
+ });
263878
264195
  try {
263879
264196
  const res = await fetch(url, {
263880
264197
  headers: {
@@ -263971,7 +264288,7 @@ function buildWebSearchTool(env2) {
263971
264288
  ],
263972
264289
  // biome-ignore lint/suspicious/noExplicitAny: plain JSON Schema compatible with TypeBox TSchema
263973
264290
  parameters: webSearchSchema,
263974
- async execute(_toolCallId, params, _signal, _onUpdate) {
264291
+ async execute(_toolCallId, params, signal, _onUpdate) {
263975
264292
  const p = params;
263976
264293
  const query = p.query;
263977
264294
  const count = p.count ?? 5;
@@ -263981,18 +264298,29 @@ function buildWebSearchTool(env2) {
263981
264298
  let lastError;
263982
264299
  for (const { provider, apiKey } of providers) {
263983
264300
  try {
263984
- const results = await provider.search({
264301
+ const { results } = await provider.search({
263985
264302
  apiKey,
263986
264303
  query,
263987
264304
  count,
263988
264305
  country,
263989
- freshness
264306
+ freshness,
264307
+ signal
263990
264308
  });
264309
+ let fetchedPages = 0;
263991
264310
  if (shouldFetchContent) {
263992
264311
  for (const r2 of results) {
263993
- r2.content = await fetchPageContent(r2.link);
264312
+ r2.content = await fetchPageContent(r2.link, signal);
264313
+ fetchedPages += 1;
263994
264314
  }
263995
264315
  }
264316
+ const usage = {
264317
+ raw: {
264318
+ [provider.id]: {
264319
+ requests: 1,
264320
+ fetchedPages
264321
+ }
264322
+ }
264323
+ };
263996
264324
  return {
263997
264325
  content: [
263998
264326
  {
@@ -264000,7 +264328,9 @@ function buildWebSearchTool(env2) {
264000
264328
  text: formatSearchResults(results, provider.label)
264001
264329
  }
264002
264330
  ],
264003
- details: void 0
264331
+ details: {
264332
+ usage
264333
+ }
264004
264334
  };
264005
264335
  } catch (e2) {
264006
264336
  lastError = e2;
@@ -264036,11 +264366,11 @@ function buildWebFetchTool() {
264036
264366
  ],
264037
264367
  // biome-ignore lint/suspicious/noExplicitAny: plain JSON Schema compatible with TypeBox TSchema
264038
264368
  parameters: webFetchSchema,
264039
- async execute(_toolCallId, params, _signal, _onUpdate) {
264369
+ async execute(_toolCallId, params, signal, _onUpdate) {
264040
264370
  const p = params;
264041
264371
  const url = p.url;
264042
264372
  try {
264043
- const content = await fetchPageContent(url);
264373
+ const content = await fetchPageContent(url, signal);
264044
264374
  return {
264045
264375
  content: [{ type: "text", text: content }],
264046
264376
  details: void 0
@@ -264183,52 +264513,6 @@ function applyModelOverrides(model, provider, optionsEnv) {
264183
264513
  model.baseUrl = anthropicBaseUrl;
264184
264514
  }
264185
264515
  }
264186
- function emitStreamError(errorText) {
264187
- return [
264188
- `data: ${JSON.stringify({ type: "error", errorText })}
264189
-
264190
- `,
264191
- `data: ${JSON.stringify({ type: "finish", finishReason: "error" })}
264192
-
264193
- `,
264194
- "data: [DONE]\n\n"
264195
- ];
264196
- }
264197
- function extractToolResultText(result) {
264198
- if (result !== null && typeof result === "object") {
264199
- const r2 = result;
264200
- if (Array.isArray(r2.content) && r2.content.length > 0) {
264201
- const text = r2.content.filter((c) => c.type === "text" && typeof c.text === "string").map((c) => c.text).join("\n");
264202
- if (text.length > 0) {
264203
- return text;
264204
- }
264205
- }
264206
- }
264207
- if (typeof result === "string")
264208
- return result;
264209
- try {
264210
- return JSON.stringify(result);
264211
- } catch {
264212
- return String(result);
264213
- }
264214
- }
264215
- function usageToMessageMetadata(usage) {
264216
- return {
264217
- input_tokens: usage.input,
264218
- output_tokens: usage.output,
264219
- cache_read_input_tokens: usage.cacheRead,
264220
- cache_creation_input_tokens: usage.cacheWrite
264221
- };
264222
- }
264223
- function getUsageFromAgentEndMessages(messages) {
264224
- for (let i2 = messages.length - 1; i2 >= 0; i2--) {
264225
- const m2 = messages[i2];
264226
- if (m2.role === "assistant" && m2.usage != null) {
264227
- return m2.usage;
264228
- }
264229
- }
264230
- return void 0;
264231
- }
264232
264516
  function getErrorFromAgentEndMessages(messages) {
264233
264517
  for (let i2 = messages.length - 1; i2 >= 0; i2--) {
264234
264518
  const m2 = messages[i2];
@@ -264244,7 +264528,7 @@ function traceRawMessage(debugCwd, data, reset = false, optionsEnv) {
264244
264528
  if (!enabled)
264245
264529
  return;
264246
264530
  try {
264247
- const file = join35(debugCwd, "pi-message-stream-debug.json");
264531
+ const file = join36(debugCwd, "pi-message-stream-debug.json");
264248
264532
  if (reset && existsSync28(file))
264249
264533
  unlinkSync6(file);
264250
264534
  const type = data !== null && typeof data === "object" ? data.type : void 0;
@@ -264313,9 +264597,22 @@ function createPiRunner(options2 = {}) {
264313
264597
  if (resume.includes("/")) {
264314
264598
  return SessionManager.open(resume);
264315
264599
  }
264316
- const sessions = await SessionManager.list(cwd);
264317
- const found = sessions.find((s2) => s2.id === resume);
264318
- return found ? SessionManager.open(found.path) : SessionManager.create(cwd);
264600
+ const sessionPath2 = resolveSessionPathById(cwd, resume);
264601
+ console.error(`${LOG_PREFIX2} resume: id=${resume} path=${sessionPath2 ?? "(not found)"}`);
264602
+ if (sessionPath2) {
264603
+ if (isSessionFileTooLarge(sessionPath2)) {
264604
+ const context = extractSessionContext(sessionPath2);
264605
+ console.error(`${LOG_PREFIX2} session file too large, starting fresh${context ? " (with context)" : ""}`);
264606
+ const newMgr = SessionManager.create(cwd);
264607
+ if (context) {
264608
+ const firstId = newMgr.getEntries()[0]?.id ?? "";
264609
+ newMgr.appendCompaction(context, firstId, 0);
264610
+ }
264611
+ return newMgr;
264612
+ }
264613
+ return SessionManager.open(sessionPath2);
264614
+ }
264615
+ return SessionManager.create(cwd);
264319
264616
  }
264320
264617
  return SessionManager.create(cwd);
264321
264618
  })();
@@ -264373,165 +264670,34 @@ function createPiRunner(options2 = {}) {
264373
264670
  }
264374
264671
  try {
264375
264672
  traceRawMessage(cwd, null, true, options2.env);
264376
- let promptText = userInput;
264377
- let images;
264378
- try {
264379
- if (userInput.startsWith("[") && userInput.endsWith("]")) {
264380
- const parsed = JSON.parse(userInput);
264381
- if (Array.isArray(parsed)) {
264382
- promptText = parsed.filter((p) => p.type === "text").map((p) => p.text).join("\n");
264383
- const imageParts = parsed.filter((p) => p.type === "image");
264384
- if (imageParts.length > 0) {
264385
- images = imageParts.map((p) => ({
264386
- type: "image",
264387
- data: p.data,
264388
- mimeType: p.mimeType
264389
- }));
264390
- }
264673
+ const promptText = userInput;
264674
+ const promptPromise = session.prompt(promptText);
264675
+ const streamConverter = new PiAISDKStreamConverter({
264676
+ sessionId: session.sessionId,
264677
+ model,
264678
+ redactText: (value2) => {
264679
+ if (options2.env && Object.keys(options2.env).length > 0) {
264680
+ return redactSecrets(value2, options2.env);
264391
264681
  }
264392
- }
264393
- } catch (_e2) {
264394
- }
264395
- const promptPromise = session.prompt(promptText, images ? { images } : void 0);
264396
- const messageId = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
264397
- let hasStarted = false;
264398
- let hasFinished = false;
264399
- const imageToolUsage = { input_tokens: 0, output_tokens: 0 };
264400
- const newTextPartId = () => `text_${Date.now()}_${Math.random().toString(36).slice(2)}_${Math.random().toString(36).slice(2)}`;
264401
- let activeTextPartId = null;
264402
- let textStreamOpen = false;
264403
- const endTextStreamIfOpen = function* () {
264404
- if (textStreamOpen && activeTextPartId != null) {
264405
- yield `data: ${JSON.stringify({ type: "text-end", id: activeTextPartId })}
264406
-
264407
- `;
264408
- textStreamOpen = false;
264409
- activeTextPartId = null;
264410
- }
264411
- };
264412
- const beginTextStream = function* () {
264413
- activeTextPartId = newTextPartId();
264414
- yield `data: ${JSON.stringify({ type: "text-start", id: activeTextPartId })}
264415
-
264416
- `;
264417
- textStreamOpen = true;
264418
- };
264419
- const ensureStartEvent = async function* () {
264420
- if (!hasStarted) {
264421
- yield `data: ${JSON.stringify({ type: "start", messageId })}
264422
-
264423
- `;
264424
- yield `data: ${JSON.stringify({
264425
- type: "message-metadata",
264426
- messageMetadata: { sessionId: session.sessionId }
264427
- })}
264428
-
264429
- `;
264430
- hasStarted = true;
264431
- }
264432
- };
264433
- const finishSuccess = async function* (usage) {
264434
- yield* endTextStreamIfOpen();
264435
- const finishPayload = { type: "finish", finishReason: "stop" };
264436
- const hasImageUsage = imageToolUsage.input_tokens > 0 || imageToolUsage.output_tokens > 0;
264437
- if (usage != null || hasImageUsage) {
264438
- const base = usage != null ? usageToMessageMetadata(usage) : {};
264439
- finishPayload.messageMetadata = {
264440
- usage: {
264441
- ...base,
264442
- input_tokens: (base.input_tokens ?? 0) + imageToolUsage.input_tokens,
264443
- output_tokens: (base.output_tokens ?? 0) + imageToolUsage.output_tokens
264444
- }
264445
- };
264446
- }
264447
- yield `data: ${JSON.stringify(finishPayload)}
264448
-
264449
- `;
264450
- yield "data: [DONE]\n\n";
264451
- hasFinished = true;
264452
- };
264453
- const finishError = async function* (errorText) {
264454
- for (const chunk of emitStreamError(errorText)) {
264455
- yield chunk;
264456
- }
264457
- hasFinished = true;
264458
- };
264682
+ return value2;
264683
+ },
264684
+ normalizeToolOutput: extractToolResultText,
264685
+ getUsageFromAgentEndMessages,
264686
+ getErrorFromAgentEndMessages
264687
+ });
264459
264688
  while (!isComplete || eventQueue.length > 0) {
264460
264689
  while (eventQueue.length > 0) {
264461
264690
  const event = eventQueue.shift();
264462
264691
  traceRawMessage(cwd, event, false, options2.env);
264463
- yield* ensureStartEvent();
264464
- if (event.type === "message_start") {
264465
- const msg = event.message;
264466
- if (msg?.role === "assistant") {
264467
- yield* endTextStreamIfOpen();
264468
- }
264469
- } else if (event.type === "message_update") {
264470
- const sub = event.assistantMessageEvent;
264471
- if (sub.type === "text_start") {
264472
- yield* endTextStreamIfOpen();
264473
- yield* beginTextStream();
264474
- } else if (sub.type === "text_delta") {
264475
- let delta = sub.delta;
264476
- if (delta) {
264477
- if (options2.env && Object.keys(options2.env).length > 0) {
264478
- delta = redactSecrets(delta, options2.env);
264479
- }
264480
- if (!textStreamOpen) {
264481
- yield* beginTextStream();
264482
- }
264483
- yield `data: ${JSON.stringify({
264484
- type: "text-delta",
264485
- id: activeTextPartId,
264486
- delta
264487
- })}
264488
-
264489
- `;
264490
- }
264491
- } else if (sub.type === "toolcall_start") {
264492
- yield* endTextStreamIfOpen();
264493
- }
264494
- } else if (event.type === "tool_execution_start") {
264495
- yield* endTextStreamIfOpen();
264496
- yield `data: ${JSON.stringify({ type: "tool-input-start", toolCallId: event.toolCallId, toolName: event.toolName, dynamic: true, providerExecuted: true })}
264497
-
264498
- `;
264499
- yield `data: ${JSON.stringify({ type: "tool-input-available", toolCallId: event.toolCallId, toolName: event.toolName, input: event.args, dynamic: true, providerExecuted: true })}
264500
-
264501
- `;
264502
- } else if (event.type === "tool_execution_end") {
264503
- let output = extractToolResultText(event.result);
264504
- if (options2.env && Object.keys(options2.env).length > 0) {
264505
- output = redactSecrets(output, options2.env);
264506
- }
264507
- if ((event.toolName === "generate_image" || event.toolName === "edit_image") && event.result !== null && typeof event.result === "object") {
264508
- const details = event.result.details;
264509
- const u = details?.response?.usage;
264510
- if (u) {
264511
- imageToolUsage.input_tokens += u.input_tokens ?? 0;
264512
- imageToolUsage.output_tokens += u.output_tokens ?? 0;
264513
- }
264514
- }
264515
- yield `data: ${JSON.stringify({ type: "tool-output-available", toolCallId: event.toolCallId, output, isError: event.isError, dynamic: true, providerExecuted: true })}
264516
-
264517
- `;
264518
- } else if (event.type === "agent_end") {
264519
- if (aborted) {
264520
- yield* finishError("Run aborted by signal.");
264521
- } else {
264522
- const errorMsg = getErrorFromAgentEndMessages(event.messages);
264523
- if (errorMsg) {
264524
- yield* finishError(errorMsg);
264525
- } else {
264526
- const usage = getUsageFromAgentEndMessages(event.messages);
264527
- yield* finishSuccess(usage);
264528
- }
264529
- }
264692
+ const chunks = streamConverter.handleEvent(event, aborted);
264693
+ for (const chunk of chunks) {
264694
+ yield chunk;
264530
264695
  }
264531
264696
  }
264532
- if (aborted && !hasFinished) {
264533
- yield* ensureStartEvent();
264534
- yield* finishError("Run aborted by signal.");
264697
+ if (aborted && !streamConverter.finished) {
264698
+ for (const chunk of streamConverter.forceError("Run aborted by signal.")) {
264699
+ yield chunk;
264700
+ }
264535
264701
  break;
264536
264702
  }
264537
264703
  if (!isComplete && eventQueue.length === 0) {
@@ -264540,22 +264706,24 @@ function createPiRunner(options2 = {}) {
264540
264706
  });
264541
264707
  }
264542
264708
  }
264543
- if (hasFinished) {
264709
+ if (streamConverter.finished) {
264544
264710
  return;
264545
264711
  }
264546
264712
  try {
264547
264713
  await promptPromise;
264548
264714
  } catch (error) {
264549
- if (!hasFinished) {
264550
- yield* ensureStartEvent();
264715
+ if (!streamConverter.finished) {
264551
264716
  const message = error instanceof Error ? error.message : "Pi agent run failed.";
264552
- yield* finishError(message);
264717
+ for (const chunk of streamConverter.forceError(message)) {
264718
+ yield chunk;
264719
+ }
264553
264720
  }
264554
264721
  return;
264555
264722
  }
264556
- if (!hasFinished && session.agent.state.error) {
264557
- yield* ensureStartEvent();
264558
- yield* finishError(session.agent.state.error);
264723
+ if (!streamConverter.finished && session.agent.state.error) {
264724
+ for (const chunk of streamConverter.forceError(session.agent.state.error)) {
264725
+ yield chunk;
264726
+ }
264559
264727
  }
264560
264728
  } finally {
264561
264729
  if (abortSignal) {
@@ -264575,11 +264743,11 @@ function createPiRunner(options2 = {}) {
264575
264743
 
264576
264744
  // ../../packages/runner-harness/dist/session.js
264577
264745
  import { existsSync as existsSync29, mkdirSync as mkdirSync11, readFileSync as readFileSync23, writeFileSync as writeFileSync14 } from "node:fs";
264578
- import { join as join36 } from "node:path";
264746
+ import { join as join37 } from "node:path";
264579
264747
  var DIR = ".bunny-agent";
264580
264748
  var FILE = "session-id";
264581
264749
  function sessionPath(cwd) {
264582
- return join36(cwd, DIR, FILE);
264750
+ return join37(cwd, DIR, FILE);
264583
264751
  }
264584
264752
  function readSessionId(cwd) {
264585
264753
  try {
@@ -264593,28 +264761,28 @@ function readSessionId(cwd) {
264593
264761
  }
264594
264762
  function writeSessionId(cwd, id) {
264595
264763
  try {
264596
- mkdirSync11(join36(cwd, DIR), { recursive: true });
264764
+ mkdirSync11(join37(cwd, DIR), { recursive: true });
264597
264765
  writeFileSync14(sessionPath(cwd), id, "utf8");
264598
264766
  } catch {
264599
264767
  }
264600
264768
  }
264601
264769
 
264602
264770
  // ../../packages/runner-harness/dist/skills.js
264603
- import { existsSync as existsSync30, readdirSync as readdirSync12, statSync as statSync13 } from "node:fs";
264771
+ import { existsSync as existsSync30, readdirSync as readdirSync13, statSync as statSync14 } from "node:fs";
264604
264772
  import { homedir as homedir14 } from "node:os";
264605
- import { join as join37 } from "node:path";
264773
+ import { join as join38 } from "node:path";
264606
264774
  function discoverSkillPaths(cwd) {
264607
264775
  const paths = [];
264608
264776
  for (const base of [
264609
- join37(cwd, "skills"),
264610
- join37(homedir14(), ".bunny-agent", "skills")
264777
+ join38(cwd, "skills"),
264778
+ join38(homedir14(), ".bunny-agent", "skills")
264611
264779
  ]) {
264612
264780
  if (!existsSync30(base))
264613
264781
  continue;
264614
264782
  try {
264615
- for (const entry of readdirSync12(base)) {
264616
- const full = join37(base, entry);
264617
- if (statSync13(full).isDirectory() && existsSync30(join37(full, "SKILL.md"))) {
264783
+ for (const entry of readdirSync13(base)) {
264784
+ const full = join38(base, entry);
264785
+ if (statSync14(full).isDirectory() && existsSync30(join38(full, "SKILL.md"))) {
264618
264786
  paths.push(full);
264619
264787
  }
264620
264788
  }