@coolkiller007/my-page-agent 0.2.7 → 0.2.8

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/index.d.ts CHANGED
@@ -44,7 +44,7 @@ declare interface AgentConfig extends LLMConfig {
44
44
  maxSteps?: number;
45
45
  /**
46
46
  * 拼装 LLM 提示词时,最近保留完整详情的历史步数;更早的步骤会被压缩成一行。
47
- * @default 15
47
+ * @default 5
48
48
  */
49
49
  historyWindow?: number;
50
50
  /**
@@ -272,6 +272,9 @@ declare class AgentRuntime extends EventTarget {
272
272
  * 图片附件缓存(按附件名索引):所有出现过的图片附件在此登记原始 dataUrl,
273
273
  * 使原图在 prompt 里只出现一次之后,仍可被 `image_understand` 工具按名称"按需重传"重新查看,
274
274
  * 而不必每步都把原图重新塞进主对话历史(节省 token)。
275
+ * @note 跨任务持久保留(不随 `execute()` 清空):缓存本身是纯内存 Map,不进 prompt 就不占 token,
276
+ * 只有模型主动调用 `image_understand` 时才会产生一次性 token 成本。唯一真实开销是内存,
277
+ * 因此按 `MAX_CACHED_IMAGES` 做容量上限,超出淘汰最早插入的一张(见 `#cacheImageAttachment`)。
275
278
  * 非私有字段:需要被 `tools/index.ts` 中的 `image_understand` 工具从外部读取。
276
279
  */
277
280
  imageAttachmentCache: Map<string, Attachment>;
package/dist/index.js CHANGED
@@ -4407,6 +4407,8 @@ var ATTACHMENT_IMAGE_MAX_DIMENSION = 1568;
4407
4407
  var ATTACHMENT_IMAGE_JPEG_QUALITY = .85;
4408
4408
  /** 单次注入里最多内联发送的图片张数,超出的仅登记缓存,需模型主动调用 image_understand 查看 */
4409
4409
  var MAX_INLINE_IMAGES = 4;
4410
+ /** 图片缓存跨任务最多保留的张数,超出时淘汰最早插入的一张(防止长会话内存无限增长;缓存本身不占 prompt token) */
4411
+ var MAX_CACHED_IMAGES = 20;
4410
4412
  /** 历史任务 done 文本注入 prompt 时的最大字符数 */
4411
4413
  var PREVIOUS_TASK_TEXT_MAX_LENGTH = 300;
4412
4414
  /**
@@ -4487,6 +4489,9 @@ var AgentRuntime = class extends EventTarget {
4487
4489
  * 图片附件缓存(按附件名索引):所有出现过的图片附件在此登记原始 dataUrl,
4488
4490
  * 使原图在 prompt 里只出现一次之后,仍可被 `image_understand` 工具按名称"按需重传"重新查看,
4489
4491
  * 而不必每步都把原图重新塞进主对话历史(节省 token)。
4492
+ * @note 跨任务持久保留(不随 `execute()` 清空):缓存本身是纯内存 Map,不进 prompt 就不占 token,
4493
+ * 只有模型主动调用 `image_understand` 时才会产生一次性 token 成本。唯一真实开销是内存,
4494
+ * 因此按 `MAX_CACHED_IMAGES` 做容量上限,超出淘汰最早插入的一张(见 `#cacheImageAttachment`)。
4490
4495
  * 非私有字段:需要被 `tools/index.ts` 中的 `image_understand` 工具从外部读取。
4491
4496
  */
4492
4497
  imageAttachmentCache = /* @__PURE__ */ new Map();
@@ -4609,7 +4614,6 @@ var AgentRuntime = class extends EventTarget {
4609
4614
  this.taskId = uid();
4610
4615
  this.#taskAttachments = attachments;
4611
4616
  this.pendingAnswerAttachments = [];
4612
- this.imageAttachmentCache = /* @__PURE__ */ new Map();
4613
4617
  this.#taskHistoryStart = this.history.length;
4614
4618
  this.#emitHistoryChange({
4615
4619
  type: "task_start",
@@ -4862,7 +4866,8 @@ var AgentRuntime = class extends EventTarget {
4862
4866
  /**
4863
4867
  * 汇总当前任务之前、同一 AgentRuntime 实例已完成过的历史任务,生成一段纯文字摘要。
4864
4868
  * 只带文字结论(任务描述 + done 文本 + 涉及的附件文件名),不携带图片原始数据 ——
4865
- * 图片附件与 `imageAttachmentCache` 一样按任务隔离,跨任务需要复看时应提示用户重新上传。
4869
+ * 图片附件是否仍可复看取决于是否还在 `imageAttachmentCache`(跨任务持久、有容量上限)里:
4870
+ * 还在则提示模型改用 `image_understand` 复看;已被淘汰(或本就是非图片附件)则提示用户重新上传。
4866
4871
  * 只取最近 `PREVIOUS_TASKS_SUMMARY_LIMIT` 个任务,避免任务数增多后 prompt 无限增长。
4867
4872
  */
4868
4873
  #buildPreviousTasksSummary() {
@@ -4883,12 +4888,25 @@ var AgentRuntime = class extends EventTarget {
4883
4888
  const recent = summaries.slice(-5);
4884
4889
  if (recent.length === 0) return "";
4885
4890
  return `<previous_tasks_summary>\n${recent.map((s) => {
4886
- const attachmentNote = s.attachmentNames?.length ? ` [attachments uploaded then: ${s.attachmentNames.join(", ")} — NOT available in this task, ask the user to re-upload if you need to see them again]` : "";
4891
+ const attachmentNote = s.attachmentNames?.length ? ` ${this.#describeAttachmentAvailability(s.attachmentNames)}` : "";
4887
4892
  const result = s.result ? truncate(s.result, PREVIOUS_TASK_TEXT_MAX_LENGTH) : "(no result recorded — task may have been interrupted)";
4888
4893
  return `- Task: "${s.task}"${attachmentNote}\n Result: ${result}`;
4889
4894
  }).join("\n")}\n</previous_tasks_summary>\n\n`;
4890
4895
  }
4891
4896
  /**
4897
+ * 按附件名分成"仍在 `imageAttachmentCache` 里、可 `image_understand` 复看"与
4898
+ * "已不可用、需用户重新上传"两组,拼成一句注入 prompt 的提示。
4899
+ */
4900
+ #describeAttachmentAvailability(attachmentNames) {
4901
+ const viewable = [];
4902
+ const unavailable = [];
4903
+ for (const name of attachmentNames) (this.imageAttachmentCache.has(name) ? viewable : unavailable).push(name);
4904
+ const parts = [];
4905
+ if (viewable.length > 0) parts.push(`still viewable via image_understand: ${viewable.join(", ")}`);
4906
+ if (unavailable.length > 0) parts.push(`NOT available in this task, ask the user to re-upload if you need to see them again: ${unavailable.join(", ")}`);
4907
+ return `[attachments uploaded then — ${parts.join("; ")}]`;
4908
+ }
4909
+ /**
4892
4910
  * 在每一步之前生成系统观察
4893
4911
  * @todo 控制台错误
4894
4912
  */
@@ -4983,6 +5001,17 @@ var AgentRuntime = class extends EventTarget {
4983
5001
  return prompt;
4984
5002
  }
4985
5003
  /**
5004
+ * 把图片附件登记进 `imageAttachmentCache`;超出 `MAX_CACHED_IMAGES` 容量时淘汰最早插入的一张。
5005
+ * 缓存跨任务持久,容量上限只为控内存,不影响 token(未被 `image_understand` 读取的缓存项不进 prompt)。
5006
+ */
5007
+ #cacheImageAttachment(attachment) {
5008
+ if (!this.imageAttachmentCache.has(attachment.name) && this.imageAttachmentCache.size >= MAX_CACHED_IMAGES) {
5009
+ const oldestName = this.imageAttachmentCache.keys().next().value;
5010
+ if (oldestName !== void 0) this.imageAttachmentCache.delete(oldestName);
5011
+ }
5012
+ this.imageAttachmentCache.set(attachment.name, attachment);
5013
+ }
5014
+ /**
4986
5015
  * 把附件内容注入 prompt:图片转为 image_url 片段;文本类文件解码后截断拼进文字部分。
4987
5016
  * 仅当存在图片附件时才返回多模态数组,纯文本附件保持返回字符串(不影响不支持 vision 的模型)。
4988
5017
  * 图片附件会先登记进 `imageAttachmentCache`(存原图,供 `image_understand` 复看细节用),
@@ -4996,7 +5025,7 @@ var AgentRuntime = class extends EventTarget {
4996
5025
  let inlinedImageCount = 0;
4997
5026
  for (const attachment of attachments) {
4998
5027
  if (attachment.mimeType.startsWith("image/")) {
4999
- this.imageAttachmentCache.set(attachment.name, attachment);
5028
+ this.#cacheImageAttachment(attachment);
5000
5029
  if (inlinedImageCount >= MAX_INLINE_IMAGES) {
5001
5030
  textSegments.push(`[Image attachment "${attachment.name}" was NOT shown inline (too many images in this batch). Call image_understand with attachment_name="${attachment.name}" if you need to see it.]`);
5002
5031
  continue;