From 4e72b16bd546e347d05e38ababe4d7fb8eac9b5c Mon Sep 17 00:00:00 2001 From: OhYee Date: Sat, 25 Apr 2026 13:58:44 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix(sdk):=20=E4=BF=AE=E5=A4=8D=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=BA=A7=20config=20=E5=9C=A8=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E9=93=BE=E4=B8=AD=E8=A2=AB=E6=82=84=E6=82=84=E4=B8=A2=E5=BC=83?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前调用方传入的 config 在多个资源链路上都未真正生效: data-api 的 timeout/ControlAPI 仍读 this.config,sandbox-data 的 withConfigs 顺序反向导致实例 config 反向覆盖方法级 config,model 和 credential 的 client 把结果 Object.assign 到 new ModelProxy() 但从未设置 _config,endpoint/template 的实例 get 也漏掉 config ?? this._config 的 fallback。结果是用户带 config 创建资源 后再链式 update/get/delete 会回落到默认环境,行为与 AgentRuntime 等已规范模块不一致。 本次统一收敛 method > instance > default 的合并优先级,让所有 请求真正遵循调用方传入的 config: - src/utils/data-api.ts: makeRequest/postFile/getFile 改用合并后 的 cfg.timeout;取 access token 的 ControlAPI 也用 cfg 构造 - src/sandbox/api/sandbox-data.ts: 反转 Config.withConfigs 入参 顺序;createSandbox/deleteSandbox/stopSandbox/getSandbox 在 refreshAccessToken 之后把 params.config 透传给后续 HTTP 调用 - src/model/client.ts: create/delete/update/get/list 10 处把 Object.assign(new X(), result) 替换为 new X(result, cfg) - src/model/model-proxy.ts, src/model/model-service.ts: 构造函数 签名扩展为 (data?, config?),保持无参兼容;实例 update/delete/ get 补 config ?? this._config fallback - src/credential/client.ts: new Credential(result) 全部改为 new Credential(result, cfg) - src/agent-runtime/endpoint.ts, src/sandbox/template.ts: 实例 get 补 config ?? this._config fallback - src/sandbox/sandbox.ts: Sandbox.get 静态方法把合并后的 cfg 传给 fromInnerObject 和各模板子类构造,避免返回的 Sandbox 丢失 config - src/toolset/toolset.ts: toApiSet/callToolAsync/listToolsAsync 统一以 params?.config ?? this._config 作为 baseConfig 修复后线上行为从"偷偷回落到默认环境"变为"真正遵循方法级 config", 原本被资源 client 静默吞掉的 endpoint/token/headers 现在都会按 预期生效。 Change-Id: If9961fabaa0830aa1d536e09c4b04b74ee97bcc1 Co-developed-by: Claude Signed-off-by: OhYee --- src/agent-runtime/endpoint.ts | 2 +- src/credential/client.ts | 8 +++--- src/model/client.ts | 44 ++++++++------------------------- src/model/model-proxy.ts | 16 ++++++++---- src/model/model-service.ts | 16 ++++++++---- src/sandbox/api/sandbox-data.ts | 9 ++++--- src/sandbox/sandbox.ts | 10 ++++---- src/sandbox/template.ts | 2 +- src/toolset/toolset.ts | 18 +++++++------- src/utils/data-api.ts | 17 +++++++------ 10 files changed, 67 insertions(+), 75 deletions(-) diff --git a/src/agent-runtime/endpoint.ts b/src/agent-runtime/endpoint.ts index 514070b..1ec5202 100644 --- a/src/agent-runtime/endpoint.ts +++ b/src/agent-runtime/endpoint.ts @@ -157,7 +157,7 @@ export class AgentRuntimeEndpoint extends ResourceBase implements AgentRuntimeEn return await AgentRuntimeEndpoint.get({ agentRuntimeId: this.agentRuntimeId!, endpointId: this.agentRuntimeEndpointId!, - config: params?.config, + config: params?.config ?? this._config, }); }; diff --git a/src/credential/client.ts b/src/credential/client.ts index 1fab832..9fbbdd1 100644 --- a/src/credential/client.ts +++ b/src/credential/client.ts @@ -84,7 +84,7 @@ export class CredentialClient { config: cfg, }); - return new Credential(result); + return new Credential(result, cfg); } catch (error) { if (error instanceof HTTPError) { throw error.toResourceError('Credential', params?.input?.credentialName); @@ -106,7 +106,7 @@ export class CredentialClient { config: cfg, }); - return new Credential(result); + return new Credential(result, cfg); } catch (error) { if (error instanceof HTTPError) { throw error.toResourceError('Credential', params?.name); @@ -165,7 +165,7 @@ export class CredentialClient { config: cfg, }); - return new Credential(result as any); + return new Credential(result as any, cfg); } catch (error) { if (error instanceof HTTPError) { throw error.toResourceError('Credential', params?.name); @@ -187,7 +187,7 @@ export class CredentialClient { credentialName: name, config: cfg, }); - return new Credential(result as any); + return new Credential(result as any, cfg); } catch (error) { if (error instanceof HTTPError) { throw error.toResourceError('Credential', params?.name); diff --git a/src/model/client.ts b/src/model/client.ts index 065f0a9..f499da9 100644 --- a/src/model/client.ts +++ b/src/model/client.ts @@ -80,9 +80,7 @@ export class ModelClient { input: createInput, config: cfg, }); - const proxy = new ModelProxy(); - Object.assign(proxy, result); - return proxy; + return new ModelProxy(result, cfg); } else { // 处理 ModelServiceCreateInput const modelServiceInput = input as ModelServiceCreateInput; @@ -95,9 +93,7 @@ export class ModelClient { input: createInput, config: cfg, }); - const service = new ModelService(); - Object.assign(service, result); - return service; + return new ModelService(result, cfg); } } catch (e) { if (e instanceof HTTPError) { @@ -136,9 +132,7 @@ export class ModelClient { modelProxyName: name, config: cfg, }); - const proxy = new ModelProxy(); - Object.assign(proxy, result); - return proxy; + return new ModelProxy(result, cfg); } catch (e) { if (e instanceof HTTPError) { error = e; @@ -159,9 +153,7 @@ export class ModelClient { modelServiceName: name, config: cfg, }); - const service = new ModelService(); - Object.assign(service, result); - return service; + return new ModelService(result, cfg); } catch (e) { if (e instanceof HTTPError) { throw e.toResourceError('Model', name); @@ -208,9 +200,7 @@ export class ModelClient { input: updateInput, config: cfg, }); - const proxy = new ModelProxy(); - Object.assign(proxy, result); - return proxy; + return new ModelProxy(result, cfg); } catch (e) { if (e instanceof HTTPError) { throw e.toResourceError('Model', name); @@ -231,9 +221,7 @@ export class ModelClient { input: updateInput, config: cfg, }); - const service = new ModelService(); - Object.assign(service, result); - return service; + return new ModelService(result, cfg); } catch (e) { if (e instanceof HTTPError) { throw e.toResourceError('Model', name); @@ -268,9 +256,7 @@ export class ModelClient { modelProxyName: name, config: cfg, }); - const proxy = new ModelProxy(); - Object.assign(proxy, result); - return proxy; + return new ModelProxy(result, cfg); } catch (e) { if (e instanceof HTTPError) { error = e; @@ -291,9 +277,7 @@ export class ModelClient { modelServiceName: name, config: cfg, }); - const service = new ModelService(); - Object.assign(service, result); - return service; + return new ModelService(result, cfg); } catch (e) { if (e instanceof HTTPError) { throw e.toResourceError('Model', name); @@ -328,11 +312,7 @@ export class ModelClient { input: request, config: cfg, }); - return (result.items || []).map(item => { - const proxy = new ModelProxy(); - Object.assign(proxy, item); - return proxy; - }); + return (result.items || []).map(item => new ModelProxy(item, cfg)); } else { // 处理 ModelServiceListInput 或无参数(默认列出 ModelService) const modelServiceInput = (input ?? {}) as ModelServiceListInput; @@ -345,11 +325,7 @@ export class ModelClient { input: request, config: cfg, }); - return (result.items || []).map(item => { - const service = new ModelService(); - Object.assign(service, item); - return service; - }); + return (result.items || []).map(item => new ModelService(item, cfg)); } }; } diff --git a/src/model/model-proxy.ts b/src/model/model-proxy.ts index 54a93ec..434dddb 100644 --- a/src/model/model-proxy.ts +++ b/src/model/model-proxy.ts @@ -8,7 +8,7 @@ import * as _ from 'lodash'; import { Config } from '../utils/config'; -import { listAllResourcesFunction, ResourceBase } from '../utils/resource'; +import { listAllResourcesFunction, ResourceBase, updateObjectProperties } from '../utils/resource'; import { ModelAPI, ModelInfo } from './api/model-api'; import { @@ -56,11 +56,17 @@ export class ModelProxy private modelApi: ModelAPI; - constructor() { + constructor(data?: any, config?: Config) { super(); this.modelApi = new ModelAPI(this.modelInfo); this.completion = this.modelApi.completion; this.embedding = this.modelApi.embedding; + if (data) { + updateObjectProperties(this, data); + } + if (config) { + this._config = config; + } } completion: (typeof ModelAPI)['prototype']['completion']; @@ -183,7 +189,7 @@ export class ModelProxy const result = await ModelProxy.update({ name: this.modelProxyName, input, - config, + config: config ?? this._config, }); this.updateSelf(result); @@ -203,7 +209,7 @@ export class ModelProxy return await ModelProxy.delete({ name: this.modelProxyName, - config: params?.config, + config: params?.config ?? this._config, }); }; @@ -220,7 +226,7 @@ export class ModelProxy const result = await ModelProxy.get({ name: this.modelProxyName, - config: params?.config, + config: params?.config ?? this._config, }); this.updateSelf(result); diff --git a/src/model/model-service.ts b/src/model/model-service.ts index 18b5b4f..4106937 100644 --- a/src/model/model-service.ts +++ b/src/model/model-service.ts @@ -6,7 +6,7 @@ */ import { Config } from '../utils/config'; -import { listAllResourcesFunction, ResourceBase } from '../utils/resource'; +import { listAllResourcesFunction, ResourceBase, updateObjectProperties } from '../utils/resource'; import { ModelAPI } from './api/model-api'; import { @@ -48,11 +48,17 @@ export class ModelService modelType?: ModelServiceImmutableProps['modelType']; private modelApi: ModelAPI; - constructor() { + constructor(data?: any, config?: Config) { super(); this.modelApi = new ModelAPI(this.modelInfo); this.completion = this.modelApi.completion; this.embedding = this.modelApi.embedding; + if (data) { + updateObjectProperties(this, data); + } + if (config) { + this._config = config; + } } completion: (typeof ModelAPI)['prototype']['completion']; @@ -173,7 +179,7 @@ export class ModelService const result = await ModelService.update({ name: this.modelServiceName, input, - config, + config: config ?? this._config, }); this.updateSelf(result); @@ -193,7 +199,7 @@ export class ModelService return await ModelService.delete({ name: this.modelServiceName, - config: params?.config, + config: params?.config ?? this._config, }); }; @@ -210,7 +216,7 @@ export class ModelService const result = await ModelService.get({ name: this.modelServiceName, - config: params?.config, + config: params?.config ?? this._config, }); this.updateSelf(result); diff --git a/src/sandbox/api/sandbox-data.ts b/src/sandbox/api/sandbox-data.ts index 1f11f92..a3d7ab0 100644 --- a/src/sandbox/api/sandbox-data.ts +++ b/src/sandbox/api/sandbox-data.ts @@ -63,7 +63,7 @@ export class SandboxDataAPI { config?: Config; }): Promise { const { sandboxId, templateName, config } = params; - const cfg = Config.withConfigs(config, this.config); + const cfg = Config.withConfigs(this.config, config); const cacheKey = (sandboxId || templateName)!; const cachedToken = this.accessTokenMap.get(cacheKey); @@ -424,6 +424,7 @@ export class SandboxDataAPI { return this.post({ path: '/', data, + config: params.config, }); }; @@ -436,7 +437,7 @@ export class SandboxDataAPI { config: params.config, }); - return this.delete({ path: '/' }); + return this.delete({ path: '/', config: params.config }); }; /** @@ -448,7 +449,7 @@ export class SandboxDataAPI { config: params.config, }); - return this.post({ path: '/stop' }); + return this.post({ path: '/stop', config: params.config }); }; /** @@ -460,6 +461,6 @@ export class SandboxDataAPI { config: params.config, }); - return this.get({ path: '/' }); + return this.get({ path: '/', config: params.config }); }; } diff --git a/src/sandbox/sandbox.ts b/src/sandbox/sandbox.ts index 7941e70..c8e5b0c 100644 --- a/src/sandbox/sandbox.ts +++ b/src/sandbox/sandbox.ts @@ -282,7 +282,7 @@ export class Sandbox extends ResourceBase implements SandboxData { // Extract data and create Sandbox instance const data = result.data || {}; - const baseSandbox = Sandbox.fromInnerObject(data as any, config); + const baseSandbox = Sandbox.fromInnerObject(data as any, cfg); // If templateType is specified, return the appropriate subclass if (templateType) { @@ -291,25 +291,25 @@ export class Sandbox extends ResourceBase implements SandboxData { case TemplateType.CODE_INTERPRETER: { const { CodeInterpreterSandbox } = await import('./code-interpreter-sandbox'); // Pass baseSandbox instead of raw data - const sandbox = new CodeInterpreterSandbox(baseSandbox, config); + const sandbox = new CodeInterpreterSandbox(baseSandbox, cfg); return sandbox; } case TemplateType.BROWSER: { const { BrowserSandbox } = await import('./browser-sandbox'); // Pass baseSandbox instead of raw data - const sandbox = new BrowserSandbox(baseSandbox, config); + const sandbox = new BrowserSandbox(baseSandbox, cfg); return sandbox; } case TemplateType.AIO: { const { AioSandbox } = await import('./aio-sandbox'); // Pass baseSandbox instead of raw data - const sandbox = new AioSandbox(baseSandbox, config); + const sandbox = new AioSandbox(baseSandbox, cfg); return sandbox; } case TemplateType.CUSTOM: { const { CustomSandbox } = await import('./custom-sandbox'); // Pass baseSandbox instead of raw data - const sandbox = new CustomSandbox(baseSandbox, config); + const sandbox = new CustomSandbox(baseSandbox, cfg); return sandbox; } } diff --git a/src/sandbox/template.ts b/src/sandbox/template.ts index d7fd83a..476b46e 100644 --- a/src/sandbox/template.ts +++ b/src/sandbox/template.ts @@ -163,7 +163,7 @@ export class Template extends ResourceBase implements TemplateData { get = async (params: { config?: Config } = {}): Promise