@tansr/sdk 0.6.0 → 0.7.1

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.
Files changed (2) hide show
  1. package/dist/index.js +54 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -28554,6 +28554,10 @@ var TWP_FEATURE_REASONING_OFF = "reasoning-off";
28554
28554
  function sendableTwpReasoningOff(features) {
28555
28555
  return features !== void 0 && features.includes(TWP_FEATURE_REASONING_OFF);
28556
28556
  }
28557
+ var TWP_FEATURE_REASONING_CONTENT = "reasoning-content";
28558
+ function sendableTwpReasoningContent(features) {
28559
+ return features !== void 0 && features.includes(TWP_FEATURE_REASONING_CONTENT);
28560
+ }
28557
28561
 
28558
28562
  // ../providers/src/twp/signing.ts
28559
28563
  import { createHash as createHash11, createHmac, randomBytes } from "node:crypto";
@@ -28664,6 +28668,11 @@ function irMessageToTwp(message, sendImages) {
28664
28668
  if (blocks.length === 0) return null;
28665
28669
  return { role: message.role, blocks };
28666
28670
  }
28671
+ function reasoningLevelOfBudget(budget) {
28672
+ if (budget <= 1024) return "low";
28673
+ if (budget <= 4096) return "medium";
28674
+ return "high";
28675
+ }
28667
28676
  function irRequestToTwpWire(request, opts = {}) {
28668
28677
  const sendImages = opts.sendImages ?? false;
28669
28678
  const cacheHints = opts.cacheHints ?? false;
@@ -28700,7 +28709,12 @@ function irRequestToTwpWire(request, opts = {}) {
28700
28709
  // ——服务端缺席语义 = 仅既有输出钳制,零行为变化
28701
28710
  ...request.promptTokensEstimate !== void 0 && Math.ceil(request.promptTokensEstimate) >= 1 ? { promptTokensEstimate: Math.ceil(request.promptTokensEstimate) } : {},
28702
28711
  // RFC-M3 B2:思考关断出线(门开 × IR off 双判;门关恒不落键零漂移)
28703
- ...opts.reasoningOff === true && request.thinking?.off === true ? { reasoning: "off" } : {}
28712
+ ...opts.reasoningOff === true && request.thinking?.off === true ? { reasoning: "off" } : (
28713
+ // S-TH1:思考开启出线(门开 × IR budget 双判;off 恒胜 budget)——
28714
+ // budget → 档位反查(EFFORT_THINKING_BUDGET 同族阈值:≤1024 low /
28715
+ // ≤4096 medium / 其余 high),网关按方言翻译
28716
+ opts.reasoningLevels === true && request.thinking?.off !== true && typeof request.thinking?.budget === "number" && request.thinking.budget > 0 ? { reasoning: reasoningLevelOfBudget(request.thinking.budget) } : {}
28717
+ )
28704
28718
  },
28705
28719
  meta: {
28706
28720
  requestId: opts.requestId ?? ulid(),
@@ -28968,6 +28982,13 @@ var TwpStreamAssembler = class {
28968
28982
  out.push({ t: "text_delta", index: i, text: v });
28969
28983
  return out;
28970
28984
  }
28985
+ if (kind === "thinking") {
28986
+ const v = record["v"];
28987
+ if (typeof v !== "string") return out;
28988
+ out.push(...this.#ensureBlock(i, "thinking", record));
28989
+ out.push({ t: "thinking_delta", index: i, text: v });
28990
+ return out;
28991
+ }
28971
28992
  if (kind === "tool_use") {
28972
28993
  out.push(...this.#ensureBlock(i, "tool_use", record));
28973
28994
  const vJson = record["vJson"];
@@ -28991,6 +29012,8 @@ var TwpStreamAssembler = class {
28991
29012
  }
28992
29013
  if (kind === "text") {
28993
29014
  out.push({ t: "block_start", index: i, block: { t: "text", text: "" } });
29015
+ } else if (kind === "thinking") {
29016
+ out.push({ t: "block_start", index: i, block: { t: "thinking", text: "" } });
28994
29017
  } else {
28995
29018
  out.push({
28996
29019
  t: "block_start",
@@ -29120,6 +29143,7 @@ var TwpAdapter = class {
29120
29143
  );
29121
29144
  const cacheHints = sendableTwpCacheHints(this.#options.features?.());
29122
29145
  const reasoningOff = sendableTwpReasoningOff(this.#options.features?.());
29146
+ const reasoningLevels = sendableTwpReasoningContent(this.#options.features?.());
29123
29147
  try {
29124
29148
  body = JSON.stringify(
29125
29149
  irRequestToTwpWire(request, {
@@ -29131,7 +29155,8 @@ var TwpAdapter = class {
29131
29155
  ...sendImages ? { sendImages } : {},
29132
29156
  ...cacheHints ? { cacheHints } : {},
29133
29157
  ...cacheHints && platformSessionId !== void 0 ? { cacheKey: platformSessionId } : {},
29134
- ...reasoningOff ? { reasoningOff } : {}
29158
+ ...reasoningOff ? { reasoningOff } : {},
29159
+ ...reasoningLevels ? { reasoningLevels } : {}
29135
29160
  })
29136
29161
  );
29137
29162
  } catch (err) {
@@ -32050,10 +32075,30 @@ function validateTokenTierOptions(api, options) {
32050
32075
  );
32051
32076
  }
32052
32077
  }
32078
+ async function fetchTwpFeatures(base, token, fetchImpl, sessionId) {
32079
+ try {
32080
+ const response = await fetchImpl(`${base}${TWP_ENDPOINT_PATHS.heartbeat}`, {
32081
+ method: "POST",
32082
+ headers: {
32083
+ "content-type": "application/json",
32084
+ accept: "application/json",
32085
+ [HEADER_APP_TOKEN]: token
32086
+ },
32087
+ body: JSON.stringify({ client: "sdk", ...sessionId !== void 0 ? { sessionId } : {} })
32088
+ });
32089
+ if (!response.ok) return void 0;
32090
+ const raw = await response.json();
32091
+ const features = raw?.features;
32092
+ return Array.isArray(features) ? features.filter((f) => typeof f === "string") : void 0;
32093
+ } catch {
32094
+ return void 0;
32095
+ }
32096
+ }
32053
32097
  async function assemblePlatformModel(options) {
32054
32098
  const base = options.baseUrl.replace(/\/+$/, "");
32055
32099
  const innerFetch = options.fetchImpl ?? fetch;
32056
32100
  const url = `${base}${TWP_ENDPOINT_PATHS.config}`;
32101
+ const featuresPromise = fetchTwpFeatures(base, options.token, innerFetch, options.sessionId?.());
32057
32102
  let response;
32058
32103
  try {
32059
32104
  response = await innerFetch(url, {
@@ -32090,13 +32135,18 @@ async function assemblePlatformModel(options) {
32090
32135
  `App config bundle from ${url} has an unexpected shape. Check that "baseUrl" points to the tansr API.`
32091
32136
  );
32092
32137
  }
32138
+ const features = await featuresPromise;
32093
32139
  const registry = new ProviderRegistry(appBundleRegistryConfig(bundle, base), {
32094
32140
  // 占位哑值:满足装配期 readApiKey 非空校验;真实鉴权恒由 fetch 包装注入
32095
32141
  env: { [APP_TOKEN_PLACEHOLDER_ENV]: APP_TOKEN_PLACEHOLDER_VALUE },
32096
32142
  fetchImpl: createAppTokenFetch(options.token, options.fetchImpl),
32097
32143
  // 拍板⑤:平台会话 id 供给经既有 TwpClientOptions.sessionId 缝出线——
32098
- // registry 为会话私有(每次装配新建),setModel/Task 子代理同缝同值
32099
- ...options.sessionId !== void 0 ? { twp: { sessionId: options.sessionId } } : {}
32144
+ // registry 为会话私有(每次装配新建),setModel/Task 子代理同缝同值。
32145
+ // S-TH3:features 能力串同缝供给(闭包定格;undefined = 门全关同现状)
32146
+ twp: {
32147
+ ...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {},
32148
+ features: () => features
32149
+ }
32100
32150
  });
32101
32151
  const built = buildClientFromRegistry(registry, options.model ?? "main", options.onProviderSelected);
32102
32152
  return { ...built, registry, capabilities: bundle.capabilities, platformModels: bundle.platformModels };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tansr/sdk",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Tansr public TypeScript SDK: headless agent API (query / createSession / runAgent) + pure protocol type re-exports (public, MIT).",
5
5
  "license": "MIT",
6
6
  "type": "module",