> For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt.

# updateWidget

更新指定 Widget 卡片实例的渲染数据。

用于卡片内操作后刷新卡片展示、把服务端最新结果写回卡片，或由 Page 根据传入的
`widgetInstanceId` 更新原卡片；需要替换卡片类型时可同时传入新的 `widgetId`。

`widgetData` 与 `toolResult` 二选一。传入 `toolResult` 时不能切换 `widgetId`，
结构化结果必须符合当前应用的 Entity 协议。切换 `widgetId` 时不能关闭重新创建。

## 扫码预览

![扫码预览 updateWidget](https://api.qrserver.com/v1/create-qr-code/?size=200x200\&data=doubao%3A%2F%2Fdoubao_apps%3Fapp_id%3Ddb_1McM5Ni%26path%3D%252Fpages%252Fapi%252Fopen%252Fupdate-widget%252Findex)

## 支持版本

前端库版本不低于 `0.0.26`。

## 支持平台

<table>
<thead>
<tr><th>平台</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>Android</td><td>支持</td></tr>
<tr><td>iOS</td><td>支持</td></tr>
<tr><td>PC</td><td>不支持</td></tr>
<tr><td>HarmonyOS</td><td>不支持</td></tr>
</tbody>
</table>

## 支持场景

<table>
<thead>
<tr><th>场景</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>页面</td><td>支持</td></tr>
<tr><td>卡片</td><td>支持</td></tr>
</tbody>
</table>

## 接入准备

### 前置条件

- **Android**：在卡片或消息环境中调用，需传入有效的 widgetInstanceId
- **iOS**：在卡片或消息环境中调用，需传入有效的 widgetInstanceId

## 使用说明

- widgetData 和 toolResult 互斥；应用插件支持 toolResult 和 reloadWidget，旧版 Applet Widget 仅支持 widgetData。toolResult 更新模型上下文并须符合 Entity 协议，不能同时切换 widgetId。reloadWidget 默认为 true；切换 widgetId 时不能设为 false。

## 调用方式

### 异步 API

```typescript
updateWidget(params: UpdateWidgetParams): Promise<object>
```

## 入参

<table>
<thead>
<tr><th>名称</th><th>类型</th><th>必填</th><th>默认值</th><th>约束</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td><code>reloadWidget</code></td><td><code>boolean</code></td><td>否</td><td><code>true</code></td><td>-</td><td>是否重新创建 Widget 组件；设为 `false` 时仅更新同一 Widget 的数据。<br>仅应用插件支持；旧版 Applet Widget 使用 IM 更新，不支持此参数。</td></tr>
<tr><td><code>widgetInstanceId</code></td><td><code>string</code></td><td>是</td><td>-</td><td>-</td><td>需要更新的 Widget 实例 ID。<br><br>卡片内可通过 `getWidgetInstanceId()` 获取；页面中需要由路由参数或业务数据传入。</td></tr>
<tr><td><code>widgetData</code></td><td><code>string</code></td><td>否</td><td>-</td><td>-</td><td>新的卡片渲染数据，通常传入 `JSON.stringify(viewData)`；不会更新模型上下文。</td></tr>
<tr><td><code>widgetId</code></td><td><code>string</code></td><td>否</td><td><code>-</code></td><td>-</td><td>可选的新 widgetId。传入后会把当前实例切换成另一个已注册的 Widget。</td></tr>
<tr><td><code>toolResult</code></td><td><code>ToolResult</code></td><td>否</td><td>-</td><td>-</td><td>符合 Entity 协议的 ToolResult；仅应用插件支持，更新卡片并同步模型上下文。</td></tr>
<tr><td><code>toolResult.content</code></td><td><code>Record&lt;string, unknown&gt;[]</code></td><td>否</td><td><code>-</code></td><td>-</td><td>Tool 返回的内容块。</td></tr>
<tr><td><code>toolResult.isError</code></td><td><code>boolean</code></td><td>否</td><td><code>false</code></td><td>-</td><td>Tool 执行是否返回错误结果。</td></tr>
<tr><td><code>toolResult.structuredContent</code></td><td><code>unknown</code></td><td>否</td><td><code>-</code></td><td>-</td><td>Tool 返回的结构化内容。</td></tr>
</tbody>
</table>

## 调用示例

```typescript
import { updateWidget } from '@doubao-dev/framework/api';
import { getWidgetInstanceId } from '@doubao-dev/framework';

await updateWidget({
  widgetInstanceId: getWidgetInstanceId(),
  widgetData: JSON.stringify({ title: '更新后的标题', status: 'done' })
});
```

## 成功返回

无返回字段。

## 错误处理

错误对象和通用错误码见 [通用错误处理](/apps-plugin/docs/js-api/common-errors.md#错误返回)。

### 失败示例

```json
{
  "errNo": 104,
  "errMsg": "invalid parameter"
}
```

### 错误码

<table>
<thead>
<tr><th>errNo</th><th>errMsg</th><th>平台</th><th>触发条件</th><th>处理建议</th></tr>
</thead>
<tbody>
<tr><td><code>103</code></td><td><code>feature not support</code></td><td>Android、iOS</td><td>当前豆包环境未提供消息更新能力，或旧版 Applet Widget 传入 reloadWidget/toolResult</td><td>在支持的环境调用；旧版 Applet Widget 只传 widgetData。</td></tr>
<tr><td><code>104</code></td><td><code>invalid parameter</code></td><td>Android、iOS</td><td>widgetInstanceId 为空、widgetData 与 toolResult 未二选一、reloadWidget 类型错误，或切换 widgetId 时关闭重建</td><td>修正参数后重试。</td></tr>
<tr><td><code>116</code></td><td><code>resource not found</code></td><td>Android、iOS</td><td>依据 widgetInstanceId 找不到对应消息，或依据 widgetId 找不到已注册的 Widget</td><td>确认 widgetInstanceId 与 widgetId 有效后重试。</td></tr>
</tbody>
</table>

#### 通用错误码

<table>
<thead>
<tr><th>errNo</th><th>errMsg</th><th>说明</th><th>平台</th></tr>
</thead>
<tbody>
<tr><td><code>102</code></td><td><code>internal error</code></td><td>内部错误</td><td>Android、iOS</td></tr>
</tbody>
</table>

## 平台差异

- 参数为空返回 104；消息或 Widget 未找到返回 116；当前豆包环境未提供消息更新能力时返回 103；其他失败统一返回 102。
