page-agent-sdk 3.18.0 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -113,7 +113,11 @@ async function C(e, t = {}) {
113
113
  temperature: t.temperature ?? e.temperature,
114
114
  maxTokens: t.maxTokens ?? e.maxTokens,
115
115
  ...e.baseUrl ? { anthropicApiUrl: x(e.baseUrl) } : {},
116
- ...e.extraConfig ? { clientOptions: e.extraConfig } : {}
116
+ ...e.extraConfig ? { clientOptions: e.extraConfig } : {},
117
+ ...e.cacheControl ? { invocationKwargs: { cache_control: {
118
+ type: "ephemeral",
119
+ ...e.cacheControl === !0 ? {} : { ttl: e.cacheControl }
120
+ } } } : {}
117
121
  });
118
122
  }
119
123
  //#endregion
@@ -154,10 +158,14 @@ function O(e) {
154
158
  let t = E(e);
155
159
  if (!t || typeof t != "object") return null;
156
160
  let n = t, r = Number(n.prompt_tokens ?? n.promptTokens ?? n.input_tokens ?? n.inputTokens ?? 0) || 0, i = Number(n.completion_tokens ?? n.completionTokens ?? n.output_tokens ?? n.outputTokens ?? 0) || 0, a = Number(n.total_tokens ?? n.totalTokens ?? r + i) || 0;
157
- return !r && !i && !a ? null : {
161
+ if (!r && !i && !a) return null;
162
+ let o = n.input_token_details, s = Number(n.cache_read_input_tokens ?? n.cacheReadInputTokens ?? o?.cache_read ?? 0) || 0, c = Number(n.cache_creation_input_tokens ?? n.cacheCreationInputTokens ?? o?.cache_creation ?? 0) || 0;
163
+ return {
158
164
  prompt_tokens: r,
159
165
  completion_tokens: i,
160
- total_tokens: a
166
+ total_tokens: a,
167
+ ...s ? { cache_read_input_tokens: s } : {},
168
+ ...c ? { cache_creation_input_tokens: c } : {}
161
169
  };
162
170
  }
163
171
  //#endregion
@@ -12420,7 +12428,7 @@ function f_(e, n) {
12420
12428
  e.debug && !l && console.warn("[page-agent-sdk][summarization] 未构造 llmInvoke(apiKey 缺失?),摘要回退零成本索引摘要");
12421
12429
  let v = /* @__PURE__ */ ln([]), y = Xp(e.vfs?.initialFiles, {
12422
12430
  persist: s ? { save: (e) => {
12423
- G.sessionId && s && s.save(n, G.sessionId, { vfs: e });
12431
+ Ft({ vfs: e });
12424
12432
  } } : void 0,
12425
12433
  maxBytes: e.vfs?.maxBytes,
12426
12434
  poolBytes: e.vfs?.poolBytes
@@ -12800,7 +12808,11 @@ function f_(e, n) {
12800
12808
  }
12801
12809
  let St = /* @__PURE__ */ Cn([]);
12802
12810
  async function Ct() {
12803
- s && (St.value = (await s.listSessions(n)).sort((e, t) => t.lastAccessed - e.lastAccessed));
12811
+ if (s) try {
12812
+ St.value = (await s.listSessions(n)).sort((e, t) => t.lastAccessed - e.lastAccessed);
12813
+ } catch (t) {
12814
+ e.debug && console.warn("[page-agent-sdk][persist] refreshSessions 失败(已吞):", t);
12815
+ }
12804
12816
  }
12805
12817
  let wt = a_(), Tt = /* @__PURE__ */ new Set();
12806
12818
  function Et(e) {
@@ -12888,7 +12900,7 @@ function f_(e, n) {
12888
12900
  };
12889
12901
  },
12890
12902
  afterRound() {
12891
- Mt(), Ft();
12903
+ Mt(), Lt();
12892
12904
  },
12893
12905
  async send(e, t) {
12894
12906
  await G.initDone, t ??= {}, t?.mission && je && x.setMission(t.mission);
@@ -13025,15 +13037,15 @@ function f_(e, n) {
13025
13037
  if (a.resolve("keep_external"), G.sessionId && s) {
13026
13038
  if (je) {
13027
13039
  let e = x.getMission();
13028
- e && s.save(n, G.sessionId, { mission: e });
13040
+ e && Ft({ mission: e });
13029
13041
  }
13030
13042
  if (Me) {
13031
13043
  let e = T.getWorkingMemory();
13032
- e && s.save(n, G.sessionId, { workingMemory: e });
13044
+ e && Ft({ workingMemory: e });
13033
13045
  }
13034
13046
  if (be) {
13035
13047
  let e = w.getFocuses();
13036
- s.save(n, G.sessionId, { focus: e.length ? e : null });
13048
+ Ft({ focus: e.length ? e : null });
13037
13049
  }
13038
13050
  }
13039
13051
  y.flush?.(), await s.flush();
@@ -13042,10 +13054,12 @@ function f_(e, n) {
13042
13054
  type: "session_restored",
13043
13055
  sessionId: r,
13044
13056
  rounds: i.messages?.length ?? 0
13045
- })), e.memory && s.save(n, G.sessionId, { memory: E.get() || (typeof e.memory == "string" ? e.memory : "") }), Ct(), Nt = void 0, Pt = !1, G.infoTick.value++, r;
13057
+ })), e.memory && Ft({ memory: E.get() || (typeof e.memory == "string" ? e.memory : "") }), Ct(), Nt = void 0, Pt = !1, G.infoTick.value++, r;
13046
13058
  },
13047
13059
  resetSession: () => {
13048
- Dt(), a.resolve("keep_external"), G.sessionId = $m(), v.splice(0, v.length), y.clear?.(), b.reset([]), e.memory || E.reset(""), x.reset(), T.reset(), w.reset(), L && L.importStack([]), G.agent && (G.agent.debugLogs.value = []), s && s.createSession(G.agentId, e.session?.title, G.sessionId), st({
13060
+ Dt(), a.resolve("keep_external"), G.sessionId = $m(), v.splice(0, v.length), y.clear?.(), b.reset([]), e.memory || E.reset(""), x.reset(), T.reset(), w.reset(), L && L.importStack([]), G.agent && (G.agent.debugLogs.value = []), s && s.createSession(G.agentId, e.session?.title, G.sessionId).catch((t) => {
13061
+ e.debug && console.warn("[page-agent-sdk][persist] createSession 失败(已吞):", t);
13062
+ }), st({
13049
13063
  type: "session_restored",
13050
13064
  sessionId: G.sessionId,
13051
13065
  rounds: 0
@@ -13271,7 +13285,7 @@ function f_(e, n) {
13271
13285
  }), e.debug && console.log("[page-agent-sdk][restore] 恢复会话", G.sessionId, `${t.messages?.length ?? 0} msgs`)) : e.debug && console.log("[page-agent-sdk][restore] 会话 meta 存在但快照为空", G.sessionId);
13272
13286
  } else G.sessionId = await s.createSession(n, t.title), e.debug && console.log("[page-agent-sdk][restore] 新建会话(无历史)", G.sessionId);
13273
13287
  } else G.sessionId = await s.createSession(n, t.title);
13274
- e.memory && s.save(n, G.sessionId, { memory: E.get() || (typeof e.memory == "string" ? e.memory : "") }), Ct();
13288
+ e.memory && Ft({ memory: E.get() || (typeof e.memory == "string" ? e.memory : "") }), Ct();
13275
13289
  }
13276
13290
  async function kt() {
13277
13291
  await mt();
@@ -13309,35 +13323,49 @@ function f_(e, n) {
13309
13323
  try {
13310
13324
  let e = await l(Uf(t, g)), i = v.indexOf(n);
13311
13325
  if (i < 0) return;
13312
- v[i].content = Lf(t, r, e), Ft();
13326
+ v[i].content = Lf(t, r, e), Lt();
13313
13327
  } catch {}
13314
13328
  })();
13315
13329
  }
13316
13330
  }
13317
13331
  let Nt, Pt = !1;
13318
- function Ft() {
13332
+ function Ft(t) {
13333
+ !G.sessionId || !s || s.save(n, G.sessionId, t).catch((t) => {
13334
+ e.debug && console.warn("[page-agent-sdk][persist] save 失败(已吞):", t);
13335
+ });
13336
+ }
13337
+ function It(t, r) {
13338
+ s && s.updateTitle(n, t, r).catch((t) => {
13339
+ e.debug && console.warn("[page-agent-sdk][persist] updateTitle 失败(已吞):", t);
13340
+ });
13341
+ }
13342
+ function Lt() {
13319
13343
  if (!G.sessionId || !s) return;
13320
- let t = JSON.parse(JSON.stringify(v));
13321
- s.save(n, G.sessionId, { messages: t });
13322
- let r = G.agent?.getState?.()?.todos ?? [];
13323
- if (s.save(n, G.sessionId, { todos: r }), je) {
13344
+ if (Ft({ messages: JSON.parse(JSON.stringify(v)) }), Ft({ todos: G.agent?.getState?.()?.todos ?? [] }), je) {
13324
13345
  let e = x.getMission();
13325
- e && s.save(n, G.sessionId, { mission: e });
13346
+ e && Ft({ mission: e });
13326
13347
  }
13327
13348
  if (Me) {
13328
13349
  let e = T.getWorkingMemory();
13329
- e && s.save(n, G.sessionId, { workingMemory: e });
13350
+ e && Ft({ workingMemory: e });
13330
13351
  }
13331
13352
  if (be) {
13332
13353
  let e = w.getFocuses();
13333
- s.save(n, G.sessionId, { focus: e.length ? e : null });
13334
- }
13335
- oe && L && (s.save(n, G.sessionId, { checkpoints: L.exportStack() }), s.save(n, G.sessionId, { usage: i }));
13336
- let a = Vm(v);
13337
- a && a !== Nt && (Nt = a, s.updateTitle(n, G.sessionId, a)), e.autoTitle !== !1 && u && !Pt && v.some((e) => e.role === "user") && v.some((e) => e.role === "assistant") && (Pt = !0, (async () => {
13338
- let e = await u(v);
13339
- e && (await s.updateTitle(n, G.sessionId, e), await Ct());
13340
- })()), e.debug && console.log("[page-agent-sdk][persist] save", G.sessionId, `${v.length} msgs`);
13354
+ Ft({ focus: e.length ? e : null });
13355
+ }
13356
+ oe && L && (Ft({ checkpoints: L.exportStack() }), Ft({ usage: i }));
13357
+ let t = Vm(v);
13358
+ if (t && t !== Nt && (Nt = t, It(G.sessionId, t)), e.autoTitle !== !1 && u && !Pt && v.some((e) => e.role === "user") && v.some((e) => e.role === "assistant")) {
13359
+ Pt = !0;
13360
+ let e = G.sessionId;
13361
+ (async () => {
13362
+ try {
13363
+ let t = await u(v);
13364
+ t && G.refCount > 0 && G.sessionId === e && (It(e, t), await Ct());
13365
+ } catch {}
13366
+ })();
13367
+ }
13368
+ e.debug && console.log("[page-agent-sdk][persist] save", G.sessionId, `${v.length} msgs`);
13341
13369
  }
13342
13370
  return G.initDone = (async () => {
13343
13371
  await Ot(), await kt(), e.mcp?.length && (async () => {