@cydm/pie 1.0.22 → 1.0.24

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.
@@ -1560,12 +1560,12 @@ function Iterator(items, options) {
1560
1560
  function RequiredArray(properties) {
1561
1561
  return globalThis.Object.keys(properties).filter((key) => !IsOptional(properties[key]));
1562
1562
  }
1563
- function _Object(properties, options) {
1563
+ function _Object_(properties, options) {
1564
1564
  const required = RequiredArray(properties);
1565
1565
  const schema = required.length > 0 ? { [Kind]: "Object", type: "object", required, properties } : { [Kind]: "Object", type: "object", properties };
1566
1566
  return CreateType(schema, options);
1567
1567
  }
1568
- var Object2 = _Object;
1568
+ var Object2 = _Object_;
1569
1569
 
1570
1570
  // ../../node_modules/@sinclair/typebox/build/esm/type/promise/promise.mjs
1571
1571
  function Promise2(item, options) {
@@ -5634,6 +5634,36 @@ import Anthropic from "@anthropic-ai/sdk";
5634
5634
  function sanitizeSurrogates(text) {
5635
5635
  return text.replace(/[\uD800-\uDFFF]/g, "");
5636
5636
  }
5637
+ var ANTHROPIC_PROVIDER_HEADERS = {
5638
+ accept: "application/json",
5639
+ "anthropic-dangerous-direct-browser-access": "true",
5640
+ "anthropic-version": "2023-06-01"
5641
+ };
5642
+ function buildAnthropicHeaders(modelHeaders, runtimeHeaders, includeLongCacheBeta) {
5643
+ const merged = /* @__PURE__ */ new Map();
5644
+ for (const layer of [ANTHROPIC_PROVIDER_HEADERS, modelHeaders, runtimeHeaders]) {
5645
+ for (const [key, value] of Object.entries(layer ?? {})) {
5646
+ const normalizedKey = key.toLowerCase();
5647
+ if (normalizedKey && normalizedKey !== "anthropic-beta") {
5648
+ merged.set(normalizedKey, value);
5649
+ }
5650
+ }
5651
+ }
5652
+ const betaFeatures = includeLongCacheBeta ? ["extended-cache-ttl-2025-04-11"] : [];
5653
+ for (const layer of [modelHeaders, runtimeHeaders]) {
5654
+ for (const [key, value] of Object.entries(layer ?? {})) {
5655
+ if (key.toLowerCase() !== "anthropic-beta") continue;
5656
+ for (const feature of value.split(",")) {
5657
+ const trimmed = feature.trim();
5658
+ if (trimmed) betaFeatures.push(trimmed);
5659
+ }
5660
+ }
5661
+ }
5662
+ if (betaFeatures.length > 0) {
5663
+ merged.set("anthropic-beta", [...new Set(betaFeatures)].join(","));
5664
+ }
5665
+ return Object.fromEntries(merged);
5666
+ }
5637
5667
  function normalizeToolCallId(id) {
5638
5668
  return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
5639
5669
  }
@@ -5791,6 +5821,31 @@ function convertTools2(tools) {
5791
5821
  };
5792
5822
  });
5793
5823
  }
5824
+ function applyCacheBreakpoints(params, retention) {
5825
+ if (retention === "none") return;
5826
+ const cacheControl = {
5827
+ type: "ephemeral",
5828
+ ...retention === "long" ? { ttl: "1h" } : {}
5829
+ };
5830
+ if (Array.isArray(params.system) && params.system.length > 0) {
5831
+ const last = params.system[params.system.length - 1];
5832
+ last.cache_control = { ...cacheControl };
5833
+ }
5834
+ if (Array.isArray(params.tools) && params.tools.length > 0) {
5835
+ const last = params.tools[params.tools.length - 1];
5836
+ last.cache_control = { ...cacheControl };
5837
+ }
5838
+ for (let i = params.messages.length - 1; i >= 0; i--) {
5839
+ const msg = params.messages[i];
5840
+ if (msg.role !== "user") continue;
5841
+ if (typeof msg.content === "string") {
5842
+ msg.content = [{ type: "text", text: msg.content, cache_control: { ...cacheControl } }];
5843
+ } else if (Array.isArray(msg.content) && msg.content.length > 0) {
5844
+ msg.content[msg.content.length - 1].cache_control = { ...cacheControl };
5845
+ }
5846
+ break;
5847
+ }
5848
+ }
5794
5849
  var streamAnthropic = (model, context, options) => {
5795
5850
  const stream = new AssistantMessageEventStream();
5796
5851
  (async () => {
@@ -5813,16 +5868,18 @@ var streamAnthropic = (model, context, options) => {
5813
5868
  };
5814
5869
  try {
5815
5870
  const apiKey = options?.apiKey ?? getEnvApiKey(model.provider, model.apiKeyEnv) ?? "";
5871
+ const cacheCapability = getModelCacheCapability(model);
5872
+ const promptCacheSupported = cacheCapability.supported && isPromptCacheCapability(cacheCapability);
5873
+ const headers = buildAnthropicHeaders(
5874
+ model.headers,
5875
+ options?.headers,
5876
+ promptCacheSupported && options?.cacheRetention === "long"
5877
+ );
5816
5878
  const client = new Anthropic({
5817
5879
  apiKey,
5818
5880
  baseURL: model.baseUrl,
5819
5881
  dangerouslyAllowBrowser: true,
5820
- defaultHeaders: {
5821
- accept: "application/json",
5822
- "anthropic-dangerous-direct-browser-access": "true",
5823
- "anthropic-version": "2023-06-01",
5824
- ...model.headers || {}
5825
- }
5882
+ defaultHeaders: headers
5826
5883
  });
5827
5884
  const params = {
5828
5885
  model: model.id,
@@ -5830,16 +5887,9 @@ var streamAnthropic = (model, context, options) => {
5830
5887
  max_tokens: options?.maxTokens || Math.floor(model.maxTokens / 3) || 1024,
5831
5888
  stream: true
5832
5889
  };
5833
- const cacheCapability = model.cacheCapability ?? getModelCacheCapability(model) ?? ANTHROPIC_PROMPT_CACHE_CAPABILITY;
5834
5890
  if (context.systemPrompt) {
5835
5891
  params.system = [{ type: "text", text: sanitizeSurrogates(context.systemPrompt) }];
5836
5892
  }
5837
- if (isPromptCacheCapability(cacheCapability) && options?.cacheRetention !== "none") {
5838
- params.cache_control = {
5839
- type: "ephemeral",
5840
- ...options?.cacheRetention === "long" ? { ttl: "1h" } : {}
5841
- };
5842
- }
5843
5893
  if (options?.temperature !== void 0) {
5844
5894
  params.temperature = options.temperature;
5845
5895
  }
@@ -5859,6 +5909,9 @@ var streamAnthropic = (model, context, options) => {
5859
5909
  params.tool_choice = options.toolChoice;
5860
5910
  }
5861
5911
  }
5912
+ if (promptCacheSupported) {
5913
+ applyCacheBreakpoints(params, options?.cacheRetention);
5914
+ }
5862
5915
  options?.onPayload?.(params);
5863
5916
  const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
5864
5917
  stream.push({ type: "start", partial: output });
@@ -9462,6 +9515,7 @@ export {
9462
9515
  Type,
9463
9516
  EventStream,
9464
9517
  getModelCacheCapability,
9518
+ EMPTY_ASSISTANT_CONTENT_ERROR,
9465
9519
  streamSimple,
9466
9520
  completeSimple,
9467
9521
  MODEL_CLASS_FALLBACK_ORDER,