@tansr/sdk 0.5.0 → 0.7.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.
package/README.md CHANGED
@@ -63,7 +63,9 @@ for await (const event of session.events) {
63
63
  开发者可选:`{ delivery: { text: 'stream'|'final', thinking: 'stream'|'final'|'off' } }`
64
64
  ——文本/思考各自选流式或整段一次性,思考可整体关显(状态派生与工具卡恒不受影响,
65
65
  缺省全流式零漂移);且可**动态切换**:`view.setDelivery(delivery)` 块粒度即时生效,
66
- 切 off 自动追溯剔除既有思考(详见手册 §9.1);
66
+ 切 off 自动追溯剔除既有思考;生成面思考(产不产/预算)
67
+ `createSession({ thinking: { budget } })` + `session.setThinking(…)`(下一轮生效),
68
+ 与呈现档正交(详见手册 §9.1);
67
69
  - **权限**:`permission: { mode?, rules?, askUser? }`——ask 裁决桥到你的 UI,缺席
68
70
  fail-closed 降级 deny;
69
71
  - **计量**:`cost.usage.updated` 逐请求;终端自查 `/v1/my-usage` 恒无金额字段,
package/dist/index.d.ts CHANGED
@@ -16086,6 +16086,15 @@ interface CreateSessionOptions {
16086
16086
  system?: IRSystemSegment[];
16087
16087
  /** 缺省 8192 */
16088
16088
  maxTokens?: number;
16089
+ /**
16090
+ * 思考生成(**生成面**旋钮;呈现面走 SessionView 的 delivery 选项,两旋钮
16091
+ * 正交):逐轮透传 IRRequest.thinking(budget = 思考 token 预算,按模型
16092
+ * 方言映射)。缺省缺席 = 不注入(沿模型/适配器缺省,多数模型即不产思考)。
16093
+ * 会话中可经 setThinking 动态换,下一轮生效。
16094
+ */
16095
+ thinking?: {
16096
+ budget?: number;
16097
+ };
16089
16098
  /** 单轮 modelTurns 护栏;缺省 200(与 serve 对齐) */
16090
16099
  maxTurnsPerQuery?: number;
16091
16100
  /** 缺省 randomUUID() */
@@ -16165,6 +16174,9 @@ interface AgentSessionInit {
16165
16174
  cwd: string;
16166
16175
  sessionId: string;
16167
16176
  maxTokens: number;
16177
+ thinking: {
16178
+ budget?: number;
16179
+ } | undefined;
16168
16180
  maxTurnsPerQuery: number;
16169
16181
  compaction: QueryCompactionOptions | undefined;
16170
16182
  initialMessages: readonly IRMessage[];
@@ -16204,6 +16216,14 @@ declare class AgentSession {
16204
16216
  * 不可复用);contextWindowTokens 不传则清空(宁缺毋错)。
16205
16217
  */
16206
16218
  setModel(next: string | SetModelBinding): void;
16219
+ /**
16220
+ * 动态切换思考生成(生成面旋钮,setModel 同形制):下一轮生效,运行中轮
16221
+ * 不受影响。undefined = 回到不注入(模型缺省);呈现面(思考显示与否)
16222
+ * 走 SessionView delivery,两旋钮正交互不推导。
16223
+ */
16224
+ setThinking(thinking: {
16225
+ budget?: number;
16226
+ } | undefined): void;
16207
16227
  /**
16208
16228
  * 关闭会话(幂等):空闲立即收口(session.ended + 流终结);运行中先
16209
16229
  * abort,收口推迟到泵终态——中止轮的尾部事件(turn.aborted 等)不丢失。
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) {
@@ -32387,6 +32412,7 @@ var AgentSession = class {
32387
32412
  #system;
32388
32413
  #tools;
32389
32414
  #maxTokens;
32415
+ #thinking;
32390
32416
  #sessionId;
32391
32417
  #maxTurnsPerQuery;
32392
32418
  #compaction;
@@ -32409,6 +32435,7 @@ var AgentSession = class {
32409
32435
  this.#system = init.system;
32410
32436
  this.#tools = init.tools;
32411
32437
  this.#maxTokens = init.maxTokens;
32438
+ this.#thinking = init.thinking;
32412
32439
  this.#sessionId = init.sessionId;
32413
32440
  this.#maxTurnsPerQuery = init.maxTurnsPerQuery;
32414
32441
  this.#compaction = init.compaction;
@@ -32484,6 +32511,14 @@ var AgentSession = class {
32484
32511
  ...next.maxOutputTokens !== void 0 ? { maxOutputTokens: next.maxOutputTokens } : {}
32485
32512
  };
32486
32513
  }
32514
+ /**
32515
+ * 动态切换思考生成(生成面旋钮,setModel 同形制):下一轮生效,运行中轮
32516
+ * 不受影响。undefined = 回到不注入(模型缺省);呈现面(思考显示与否)
32517
+ * 走 SessionView delivery,两旋钮正交互不推导。
32518
+ */
32519
+ setThinking(thinking) {
32520
+ this.#thinking = thinking;
32521
+ }
32487
32522
  /**
32488
32523
  * 关闭会话(幂等):空闲立即收口(session.ended + 流终结);运行中先
32489
32524
  * abort,收口推迟到泵终态——中止轮的尾部事件(turn.aborted 等)不丢失。
@@ -32544,6 +32579,7 @@ var AgentSession = class {
32544
32579
  tools: this.#tools,
32545
32580
  model: binding.model,
32546
32581
  maxTokens: this.#maxTokens,
32582
+ ...this.#thinking !== void 0 ? { thinking: this.#thinking } : {},
32547
32583
  sessionId: this.#sessionId,
32548
32584
  initialMessages,
32549
32585
  maxTurns: this.#maxTurnsPerQuery,
@@ -32786,6 +32822,7 @@ async function createSession(options = {}) {
32786
32822
  cwd,
32787
32823
  sessionId,
32788
32824
  maxTokens,
32825
+ thinking: options.thinking,
32789
32826
  maxTurnsPerQuery: options.maxTurnsPerQuery ?? DEFAULT_MAX_TURNS,
32790
32827
  compaction: options.compaction === false ? void 0 : options.compaction ?? {},
32791
32828
  initialMessages,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tansr/sdk",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
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",