@tansr/sdk 0.7.2 → 0.8.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/dist/index.d.ts CHANGED
@@ -15864,6 +15864,84 @@ interface AssembledTooling {
15864
15864
  */
15865
15865
  declare function assembleTooling(options: AssembleToolingOptions): Promise<AssembledTooling>;
15866
15866
 
15867
+ /**
15868
+ * S-B1 — 平台令牌档装配:app_user 令牌 → bundle 拉取 → registry 轻装配。
15869
+ *
15870
+ * 三档并立(createSession/query 同判):
15871
+ * - 令牌档:`{ token, baseUrl }`(本模块)——终端分发形态,令牌短时效
15872
+ * 受限(doc/89 契约 1),能力位由 bundle 供给,**恒零本地配置 IO**;
15873
+ * - 托管档:`model: string`(五层配置 BYOK,assembly.ts);
15874
+ * - 注入档:`client + model 对象`(自带 ModelClient)。
15875
+ *
15876
+ * 令牌传输(对接方式定谳):TwpAdapter 是 AK/SK 双头 + 签名三头的自持
15877
+ * 形态,不支持「替换鉴权头」的构造选项(defaultHeaders 只能追加,双头仍
15878
+ * 出线)——故用 fetchImpl 注入缝包装(**不改 providers 包**):每次请求
15879
+ * 剥双头与签名三头、注入契约头 x-tansr-app-token。registry env 只喂占位
15880
+ * 哑值(APP_TOKEN_PLACEHOLDER_VALUE)满足装配期非空校验;令牌本体只活在
15881
+ * fetch 包装闭包,恒不进 registry/adapter 构造参数(INV-10 密钥纪律同源)。
15882
+ */
15883
+
15884
+ /**
15885
+ * 平台提示帧(TWP t.warn 同形;S-ARCH doc/109 代拍②):平台对非致命降级
15886
+ * 的结构化告知(思考不可用/余额预警等),恒不代表请求失败。
15887
+ */
15888
+ interface PlatformWarning {
15889
+ code: string;
15890
+ message: string;
15891
+ }
15892
+ /** 令牌档装配选项(createSession/query 的 token 档字段投影) */
15893
+ interface AssemblePlatformModelOptions {
15894
+ /** app_user 短期令牌(开发者服务端经 POST /v1/app-tokens 换发) */
15895
+ token: string;
15896
+ /** 平台 API 基址(如 https://api.example.com;/t1 前缀由装配追加) */
15897
+ baseUrl: string;
15898
+ /** 模型别名(bundle aliases/目录 handle);缺省 'main' */
15899
+ model?: string;
15900
+ /** fetch 注入缝(bundle 拉取与 twp 交换共用;测试桩网关) */
15901
+ fetchImpl?: typeof fetch;
15902
+ /** fallback 链最终命中通报(session 装配合成 cost.provider.switched) */
15903
+ onProviderSelected?: (selection: FallbackSelection) => void;
15904
+ /**
15905
+ * S-ARCH(doc/109 代拍②):平台 t.warn 提示帧旁路(thinking_unavailable_on_face
15906
+ * /balance_low 等)。此前 SDK 面恒缺席 = 平台的结构化告知在最后一米静默
15907
+ * 蒸发;经既有 registry twp 选项缝透传,providers 零改动。
15908
+ */
15909
+ onWarn?: (warn: PlatformWarning) => void;
15910
+ /**
15911
+ * doc/91 拍板⑤ — twp meta.sessionId 动态供给(平台会话 id,ULID 形制;
15912
+ * 经 ProviderRegistry twp 选项缝透传,providers 零改动)。createSession
15913
+ * 令牌档 per-AgentSession 铸造一枚注入;缺席 = meta.sessionId 如实不携带
15914
+ * (归账兜底在认证头,批ι 前语义),query 单轮档现维持缺席。
15915
+ */
15916
+ sessionId?: () => string | undefined;
15917
+ }
15918
+ interface AssembledPlatformModel {
15919
+ client: ModelClient;
15920
+ model: ResolvedModel;
15921
+ contextWindowTokens?: number;
15922
+ maxOutputTokens?: number;
15923
+ /** bundle 装配的 registry(setModel 别名热切换与 Task resolveModel 复用) */
15924
+ registry: ProviderRegistry;
15925
+ /** bundle capabilities 段(S-B3 裁剪的事实源;缺席时为平台缺省档) */
15926
+ capabilities: AppCapabilities;
15927
+ /**
15928
+ * bundle 授权媒体模型集(S-G1;初始化即取,媒体工具装配自取的数据源——
15929
+ * 集成方零手配)。旧 api 无段 = 双空集如实。
15930
+ */
15931
+ platformModels: AppBundlePlatformModels;
15932
+ }
15933
+ /**
15934
+ * 令牌头注入 fetch 包装:剥 AK/SK 双头与签名三头(值只是占位哑值,但
15935
+ * 恒不出线),注入 x-tansr-app-token。令牌只活在本闭包。
15936
+ */
15937
+ declare function createAppTokenFetch(token: string, inner?: typeof fetch): typeof fetch;
15938
+ /**
15939
+ * 令牌档一站式:拉 bundle(GET {baseUrl}/t1/config,令牌头鉴权)→ 解析
15940
+ * models/aliases/capabilities → registry 轻装配 → 别名 client。装配失败
15941
+ * 一律 TansrSdkError('assembly_failed') 携修复指引。
15942
+ */
15943
+ declare function assemblePlatformModel(options: AssemblePlatformModelOptions): Promise<AssembledPlatformModel>;
15944
+
15867
15945
  /** 托管/令牌档单轮选项(工具/权限缝与 CreateSessionOptions 同名同义) */
15868
15946
  interface ManagedQueryOptions {
15869
15947
  /** 模型别名(main/fast/reasoning/自定义)或 ref(provider/model);缺省 'main' */
@@ -15878,6 +15956,11 @@ interface ManagedQueryOptions {
15878
15956
  token?: string;
15879
15957
  /** 平台 API 基址(令牌档必填,如 https://api.example.com) */
15880
15958
  baseUrl?: string;
15959
+ /**
15960
+ * 平台提示帧承接(S-ARCH;语义同 CreateSessionOptions.onPlatformWarning):
15961
+ * 缺席缺省 console.warn 每码一次,在场即全量接管。
15962
+ */
15963
+ onPlatformWarning?: (warning: PlatformWarning) => void;
15881
15964
  /** 项目层配置发现起点与工具 cwd;缺省 process.cwd() */
15882
15965
  cwd?: string;
15883
15966
  /** 环境变量注入缝;缺省 process.env */
@@ -16050,6 +16133,14 @@ interface CreateSessionOptions {
16050
16133
  token?: string;
16051
16134
  /** 平台 API 基址(令牌档必填,如 https://api.example.com) */
16052
16135
  baseUrl?: string;
16136
+ /**
16137
+ * S-ARCH(doc/109 代拍②)— 平台提示帧承接(仅令牌档产生;其余档恒不
16138
+ * 触发)。平台以 t.warn 结构化告知非致命降级(如 thinking_unavailable_on_face
16139
+ * :思考被请求而当前供应商面无从外露,回答照常仅无思考;balance_low:
16140
+ * 余额预警)。缺席时缺省承接 = console.warn 每码一次(告知恒不静默蒸发);
16141
+ * 提供回调即全量接管(不去重,原始帧逐一交付,呈现策略归开发者)。
16142
+ */
16143
+ onPlatformWarning?: (warning: PlatformWarning) => void;
16053
16144
  /** 项目层配置发现起点与工具 cwd;缺省 process.cwd() */
16054
16145
  cwd?: string;
16055
16146
  /** 环境变量注入缝;缺省 process.env */
@@ -16582,70 +16673,6 @@ interface Narrator {
16582
16673
  }
16583
16674
  declare function createNarrator(source: SessionViewSource, options: NarratorOptions): Narrator;
16584
16675
 
16585
- /**
16586
- * S-B1 — 平台令牌档装配:app_user 令牌 → bundle 拉取 → registry 轻装配。
16587
- *
16588
- * 三档并立(createSession/query 同判):
16589
- * - 令牌档:`{ token, baseUrl }`(本模块)——终端分发形态,令牌短时效
16590
- * 受限(doc/89 契约 1),能力位由 bundle 供给,**恒零本地配置 IO**;
16591
- * - 托管档:`model: string`(五层配置 BYOK,assembly.ts);
16592
- * - 注入档:`client + model 对象`(自带 ModelClient)。
16593
- *
16594
- * 令牌传输(对接方式定谳):TwpAdapter 是 AK/SK 双头 + 签名三头的自持
16595
- * 形态,不支持「替换鉴权头」的构造选项(defaultHeaders 只能追加,双头仍
16596
- * 出线)——故用 fetchImpl 注入缝包装(**不改 providers 包**):每次请求
16597
- * 剥双头与签名三头、注入契约头 x-tansr-app-token。registry env 只喂占位
16598
- * 哑值(APP_TOKEN_PLACEHOLDER_VALUE)满足装配期非空校验;令牌本体只活在
16599
- * fetch 包装闭包,恒不进 registry/adapter 构造参数(INV-10 密钥纪律同源)。
16600
- */
16601
-
16602
- /** 令牌档装配选项(createSession/query 的 token 档字段投影) */
16603
- interface AssemblePlatformModelOptions {
16604
- /** app_user 短期令牌(开发者服务端经 POST /v1/app-tokens 换发) */
16605
- token: string;
16606
- /** 平台 API 基址(如 https://api.example.com;/t1 前缀由装配追加) */
16607
- baseUrl: string;
16608
- /** 模型别名(bundle aliases/目录 handle);缺省 'main' */
16609
- model?: string;
16610
- /** fetch 注入缝(bundle 拉取与 twp 交换共用;测试桩网关) */
16611
- fetchImpl?: typeof fetch;
16612
- /** fallback 链最终命中通报(session 装配合成 cost.provider.switched) */
16613
- onProviderSelected?: (selection: FallbackSelection) => void;
16614
- /**
16615
- * doc/91 拍板⑤ — twp meta.sessionId 动态供给(平台会话 id,ULID 形制;
16616
- * 经 ProviderRegistry twp 选项缝透传,providers 零改动)。createSession
16617
- * 令牌档 per-AgentSession 铸造一枚注入;缺席 = meta.sessionId 如实不携带
16618
- * (归账兜底在认证头,批ι 前语义),query 单轮档现维持缺席。
16619
- */
16620
- sessionId?: () => string | undefined;
16621
- }
16622
- interface AssembledPlatformModel {
16623
- client: ModelClient;
16624
- model: ResolvedModel;
16625
- contextWindowTokens?: number;
16626
- maxOutputTokens?: number;
16627
- /** bundle 装配的 registry(setModel 别名热切换与 Task resolveModel 复用) */
16628
- registry: ProviderRegistry;
16629
- /** bundle capabilities 段(S-B3 裁剪的事实源;缺席时为平台缺省档) */
16630
- capabilities: AppCapabilities;
16631
- /**
16632
- * bundle 授权媒体模型集(S-G1;初始化即取,媒体工具装配自取的数据源——
16633
- * 集成方零手配)。旧 api 无段 = 双空集如实。
16634
- */
16635
- platformModels: AppBundlePlatformModels;
16636
- }
16637
- /**
16638
- * 令牌头注入 fetch 包装:剥 AK/SK 双头与签名三头(值只是占位哑值,但
16639
- * 恒不出线),注入 x-tansr-app-token。令牌只活在本闭包。
16640
- */
16641
- declare function createAppTokenFetch(token: string, inner?: typeof fetch): typeof fetch;
16642
- /**
16643
- * 令牌档一站式:拉 bundle(GET {baseUrl}/t1/config,令牌头鉴权)→ 解析
16644
- * models/aliases/capabilities → registry 轻装配 → 别名 client。装配失败
16645
- * 一律 TansrSdkError('assembly_failed') 携修复指引。
16646
- */
16647
- declare function assemblePlatformModel(options: AssemblePlatformModelOptions): Promise<AssembledPlatformModel>;
16648
-
16649
16676
  /**
16650
16677
  * S-E1 — ImageGen 环3 平台工具客户端(doc/84 §4.2 平台托管能力;用户拍板
16651
16678
  * 2026-08-29「开 imageGen」)。
@@ -16930,4 +16957,4 @@ declare function attachSdkTaskTool(toolset: SdkToolsetRef, config: AttachSdkTask
16930
16957
  declare function subagentModelResolverOf(registry: ProviderRegistry | null): SubagentModelResolver | undefined;
16931
16958
 
16932
16959
  export { APP_SDK_CONTRACT_VERSION, APP_TOKEN_PLACEHOLDER_ENV, AgentSession, AppCapabilitiesSchema, AppTokenRequestSchema, AppTokenResponseSchema, DEFAULT_APP_CAPABILITIES, DEFAULT_MAX_TOKENS, DEFAULT_MAX_TURNS, EndUserIdSchema, HEADER_APP_TOKEN, IMAGEGEN_TOOL_MAX_N, IMAGEGEN_TOOL_TIMEOUT_MS, ImageGenArgsSchema, McpHost, OUTPUT_TAIL_MAX_CHARS, PLATFORM_SEARCH_PROVIDER_NAME, QueueChannel, SequentialToolExecutor, TANSR_PROVIDER_ID, TOOL_GUIDE_LABEL, TansrSdkError, UnavailableChannel, VIDEOGEN_TOOL_MAX_DURATION, VIDEOGEN_TOOL_TIMEOUT_MS, VideoGenArgsSchema, accumulateUsage, appBundleRegistryConfig, appendUserMessage, assembleManagedModel, assemblePlatformModel, assembleSdkSkills, assembleTooling, attachSdkMcp, attachSdkTaskTool, buildClientFromRegistry, buildSdkToolSet, buildToolGuideSegment, createAppTokenFetch, createFileSessionStore, createImageGenTool, createMcpHost, createNarrator, createPlatformSearchProvider, createSdkPermissionGate, createSession, createSessionView, createVideoGenTool, defineSkill, defineTool, initialSessionViewState, markSourceFailure, parseAppBundle, providerSwitchedBody, query, reduceSessionView, resolveBuiltinSelection, resolveInitialMessages, resolvePlatformSelection, runAgent, setSessionViewDelivery, stripThinkingParts, subagentModelResolverOf, toToolDef, viewStateFromHistory };
16933
- export type { ActiveToolStatus, Answer, AppBundle, AppBundleImageModel, AppBundleMediaModel, AppBundleModel, AppBundlePlatformModels, AppBundleVideoModel, AppCapabilities, AppTokenRequest, AppTokenResponse, AskUserCallback, AssembleManagedModelOptions, AssemblePlatformModelOptions, AssembleToolingOptions, AssembledManagedModel, AssembledPlatformModel, AssembledSkills, AssembledTooling, AttachSdkTaskToolConfig, BuildSdkToolSetOptions, BuiltinToolName, Capability, CompactNowOptions, CompactNowResult, ConfigDiagnostic, CreateFileSessionStoreOptions, CreateImageGenToolOptions, CreatePlatformSearchProviderOptions, CreateSdkPermissionGateOptions, CreateSessionOptions, CreateVideoGenToolOptions, DecisionSource, DefineSkillOptions, DefineToolOptions, DefinedSkill, DefinedTool, DeliveryConfig, ErrorView, EventBody, EventEnvelope, GateDecision, HistoryCommitMeta, HookOutcomeStatus, IRBlock, IRErrorKind, IRMessage, IRRequest, IRRole, IRStreamEvent, IRSystemSegment, IRToolContent, IRToolDef, IRToolResultBlock, IRUsage, ImageGenArgs, ImageGenData, ImageModelConstraints, Integrity, JournalKind, JournalRecord, KernelEvent, KernelState, LoadConfigOptions, LoadedConfig, ManagedQueryOptions, McpClientEvent, McpConnectFactory, McpConnection, McpEventObserver, McpHostOptions, McpServerConfig, McpSessionOption, ModelCallOptions, ModelClient, Narrator, NarratorOptions, NarratorVerbosity, PermissionDecision, PermissionGate, PermissionMode, PermissionOptions, PermissionRules, PlatformCapabilityName, PlatformToolContext, Pricing, PromptCachingMode, PromptChannel, ProposedToolCall, ProtocolKind, ProviderProfile, QueryCompactionOptions, QueryCounters, QueryHandle, QueryOptions, QueryResult, Question, QuestionOption, ResolvedModel, RunAgentOptions, SdkErrorCode, SdkSkillsOptions, SdkToolSet, SdkToolsOptions, SdkToolsetRef, SequentialToolHandler, SequentialToolResult, SessionEventSource, SessionRecord, SessionRecordMeta, SessionStore, SessionStoreCommitMeta, SessionStoreCreateInit, SessionView, SessionViewDeliveryOptions, SessionViewOptions, SessionViewReducerState, SessionViewSource, SessionViewState, SessionViewStatus, SetModelBinding, SkillsFileSystem, TansrConfig, TerminalReason, TextDeliveryMode, ThinkingDeliveryMode, TodoView, Tool, ToolCallPartStatus, ToolCallView, ToolContext, ToolErrorType, ToolExecutionContext, ToolExecutionOutcome, ToolExecutor, ToolExecutorYield, ToolParameterSpec, ToolResult, ToolResultContent, UIMessage, UIMessagePart, UITextPart, UIThinkingPart, UIToolCallPart, UsageView, VideoGenArgs, VideoGenData, VideoModelConstraints, Visibility };
16960
+ export type { ActiveToolStatus, Answer, AppBundle, AppBundleImageModel, AppBundleMediaModel, AppBundleModel, AppBundlePlatformModels, AppBundleVideoModel, AppCapabilities, AppTokenRequest, AppTokenResponse, AskUserCallback, AssembleManagedModelOptions, AssemblePlatformModelOptions, AssembleToolingOptions, AssembledManagedModel, AssembledPlatformModel, AssembledSkills, AssembledTooling, AttachSdkTaskToolConfig, BuildSdkToolSetOptions, BuiltinToolName, Capability, CompactNowOptions, CompactNowResult, ConfigDiagnostic, CreateFileSessionStoreOptions, CreateImageGenToolOptions, CreatePlatformSearchProviderOptions, CreateSdkPermissionGateOptions, CreateSessionOptions, CreateVideoGenToolOptions, DecisionSource, DefineSkillOptions, DefineToolOptions, DefinedSkill, DefinedTool, DeliveryConfig, ErrorView, EventBody, EventEnvelope, GateDecision, HistoryCommitMeta, HookOutcomeStatus, IRBlock, IRErrorKind, IRMessage, IRRequest, IRRole, IRStreamEvent, IRSystemSegment, IRToolContent, IRToolDef, IRToolResultBlock, IRUsage, ImageGenArgs, ImageGenData, ImageModelConstraints, Integrity, JournalKind, JournalRecord, KernelEvent, KernelState, LoadConfigOptions, LoadedConfig, ManagedQueryOptions, McpClientEvent, McpConnectFactory, McpConnection, McpEventObserver, McpHostOptions, McpServerConfig, McpSessionOption, ModelCallOptions, ModelClient, Narrator, NarratorOptions, NarratorVerbosity, PermissionDecision, PermissionGate, PermissionMode, PermissionOptions, PermissionRules, PlatformCapabilityName, PlatformToolContext, PlatformWarning, Pricing, PromptCachingMode, PromptChannel, ProposedToolCall, ProtocolKind, ProviderProfile, QueryCompactionOptions, QueryCounters, QueryHandle, QueryOptions, QueryResult, Question, QuestionOption, ResolvedModel, RunAgentOptions, SdkErrorCode, SdkSkillsOptions, SdkToolSet, SdkToolsOptions, SdkToolsetRef, SequentialToolHandler, SequentialToolResult, SessionEventSource, SessionRecord, SessionRecordMeta, SessionStore, SessionStoreCommitMeta, SessionStoreCreateInit, SessionView, SessionViewDeliveryOptions, SessionViewOptions, SessionViewReducerState, SessionViewSource, SessionViewState, SessionViewStatus, SetModelBinding, SkillsFileSystem, TansrConfig, TerminalReason, TextDeliveryMode, ThinkingDeliveryMode, TodoView, Tool, ToolCallPartStatus, ToolCallView, ToolContext, ToolErrorType, ToolExecutionContext, ToolExecutionOutcome, ToolExecutor, ToolExecutorYield, ToolParameterSpec, ToolResult, ToolResultContent, UIMessage, UIMessagePart, UITextPart, UIThinkingPart, UIToolCallPart, UsageView, VideoGenArgs, VideoGenData, VideoModelConstraints, Visibility };
package/dist/index.js CHANGED
@@ -32025,6 +32025,15 @@ function appBundleRegistryConfig(bundle, baseUrl) {
32025
32025
  }
32026
32026
 
32027
32027
  // src/platform/client.ts
32028
+ function createPlatformWarningHandler(custom) {
32029
+ if (custom !== void 0) return custom;
32030
+ const warned = /* @__PURE__ */ new Set();
32031
+ return (warning) => {
32032
+ if (warned.has(warning.code)) return;
32033
+ warned.add(warning.code);
32034
+ console.warn(`[tansr-sdk] platform warning ${warning.code}: ${warning.message}`);
32035
+ };
32036
+ }
32028
32037
  var STRIPPED_HEADERS = [
32029
32038
  TWP_KEY_ID_HEADER,
32030
32039
  TWP_KEY_HEADER,
@@ -32149,7 +32158,8 @@ async function assemblePlatformModel(options) {
32149
32158
  // S-TH3:features 能力串同缝供给(闭包定格;undefined = 门全关同现状)
32150
32159
  twp: {
32151
32160
  ...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {},
32152
- features: () => features
32161
+ features: () => features,
32162
+ ...options.onWarn !== void 0 ? { onWarn: options.onWarn } : {}
32153
32163
  }
32154
32164
  });
32155
32165
  const built = buildClientFromRegistry(registry, options.model ?? "main", options.onProviderSelected);
@@ -32236,7 +32246,8 @@ async function* consumeManaged(options) {
32236
32246
  token: options.token,
32237
32247
  baseUrl: options.baseUrl,
32238
32248
  ...options.model !== void 0 ? { model: options.model } : {},
32239
- ...options.fetchImpl !== void 0 ? { fetchImpl: options.fetchImpl } : {}
32249
+ ...options.fetchImpl !== void 0 ? { fetchImpl: options.fetchImpl } : {},
32250
+ onWarn: createPlatformWarningHandler(options.onPlatformWarning)
32240
32251
  });
32241
32252
  assembled = { ...platform, loaded: null };
32242
32253
  platformContext = {
@@ -32752,7 +32763,8 @@ async function createSession(options = {}) {
32752
32763
  onProviderSelected: (selection) => {
32753
32764
  const body = providerSwitchedBody(selection);
32754
32765
  if (body !== null) emitBody(body, "provider");
32755
- }
32766
+ },
32767
+ onWarn: createPlatformWarningHandler(options.onPlatformWarning)
32756
32768
  });
32757
32769
  binding = {
32758
32770
  client: assembled.client,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tansr/sdk",
3
- "version": "0.7.2",
3
+ "version": "0.8.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",