> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # 回饋 **新增於:**`@mastra/core@1.18.0` 回饋 API 可儲存及查詢人工介入的可觀測性 signal,例如評分、拇指評價、留言與修正。如需使用模式,請參閱[回饋指南](https://mastra.zisheng.pro/zh-TW/docs/observability/feedback)。 ## 使用範例 以下範例透過可觀測性進入點,為已持久化的 trace 記錄評分。`addFeedback()` 在此進入點上為選用,因此呼叫前請先檢查作用中的可觀測性實作是否支援。 ```typescript if (!mastra.observability.addFeedback) { throw new Error('Feedback is not supported by the active observability implementation') } await mastra.observability.addFeedback({ traceId: 'trace-123', spanId: 'span-456', feedback: { feedbackSource: 'user', feedbackType: 'rating', value: 1, comment: 'Helpful answer.', }, }) ``` ## 建立回饋 ### `addFeedback(args)` 透過可觀測性進入點,將回饋新增至已持久化的 trace 或 span。 ```typescript await mastra.observability.addFeedback?.({ traceId: 'trace-123', spanId: 'span-456', feedback: { feedbackSource: 'user', feedbackType: 'rating', value: 1, comment: 'Helpful answer.', }, }) ``` **traceId** (`string`): 作為回饋目標定位依據的 trace。 **spanId** (`string`): 作為回饋目標定位依據的 span。 **correlationContext** (`CorrelationContext`): 用於產生事件的即時 span 或 trace context,不必從儲存空間重新載入目標。 **feedback** (`FeedbackInput`): 要新增的回饋 payload。 ### `createFeedback(args)` 透過可觀測性 storage domain 建立一筆回饋記錄。儲存層級呼叫會直接寫入儲存區,因此請包含 `timestamp`。 ```typescript await observability.createFeedback({ feedback: { feedbackId: 'feedback-1', timestamp: new Date(), traceId: 'trace-123', spanId: 'span-456', feedbackSource: 'user', feedbackType: 'rating', value: 1, comment: 'Helpful answer.', }, }) ``` HTTP 與 client SDK 的建立 route 接受 `CreateFeedbackBody`,並由 server 端設定 `timestamp`。省略 `feedbackId` 時會自動產生: ```typescript await mastraClient.createFeedback({ feedback: { traceId: 'trace-123', spanId: 'span-456', feedbackSource: 'user', feedbackType: 'rating', value: 1, }, }) ``` ### `batchCreateFeedback(args)` 透過可觀測性 storage domain 建立多筆回饋記錄。HTTP route 與 `@mastra/client-js` 不會公開此方法。 ```typescript await observability.batchCreateFeedback({ feedbacks: [ { feedbackId: 'feedback-1', timestamp: new Date(), traceId: 'trace-123', feedbackSource: 'user', feedbackType: 'rating', value: 1, }, { feedbackId: 'feedback-2', timestamp: new Date(), traceId: 'trace-123', feedbackSource: 'qa', feedbackType: 'comment', value: 'Needs a citation before shipping.', }, ], }) ``` ## 列出回饋 ### `listFeedback(args?)` 以分頁模式或 delta 模式傳回回饋記錄。 ```typescript const response = await mastraClient.listFeedback({ filters: { feedbackType: 'rating', feedbackSource: 'studio', }, pagination: { page: 0, perPage: 20 }, orderBy: { field: 'timestamp', direction: 'DESC' }, }) ``` **mode** (`'page' | 'delta'`): 列出模式。預設為 'page'。 **filters** (`FeedbackFilter`): 回饋記錄的 filter。 **pagination** (`{ page?: number; perPage?: number }`): 分頁模式的分頁設定。page 從零開始編號。 **orderBy** (`{ field?: 'timestamp'; direction?: 'ASC' | 'DESC' }`): 分頁模式的排序設定。 **after** (`string`): 用於增量輪詢的 delta cursor。只適用於 delta 模式。 **limit** (`number`): Delta 模式中傳回的更新數量上限。 ## OLAP 查詢 OLAP 回饋查詢會針對數值型 `value` 欄位運作。 ### `getFeedbackAggregate(args)` 傳回單一彙總回饋值。 ```typescript const response = await mastraClient.getFeedbackAggregate({ feedbackType: 'rating', feedbackSource: 'user', aggregation: 'avg', comparePeriod: 'previous_day', }) ``` **feedbackType** (`string`): 要彙總的回饋類型。 **feedbackSource** (`string`): 要彙總的回饋來源。 **aggregation** (`'sum' | 'avg' | 'min' | 'max' | 'count' | 'count_distinct' | 'last'`): 要套用的彙總方式。 **filters** (`FeedbackFilter`): 其他 filter。 **comparePeriod** (`'previous_period' | 'previous_day' | 'previous_week'`): 選用的逐期比較。 ### `getFeedbackBreakdown(args)` 傳回依維度分組的回饋值。 ```typescript const response = await mastraClient.getFeedbackBreakdown({ feedbackType: 'rating', groupBy: ['entityName'], aggregation: 'avg', }) ``` ### `getFeedbackTimeSeries(args)` 傳回依時間間隔分桶的回饋值。 ```typescript const response = await mastraClient.getFeedbackTimeSeries({ feedbackType: 'rating', interval: '1h', aggregation: 'avg', groupBy: ['feedbackSource'], }) ``` ### `getFeedbackPercentiles(args)` 傳回依時間間隔分桶的百分位數值。 ```typescript const response = await mastraClient.getFeedbackPercentiles({ feedbackType: 'rating', percentiles: [0.5, 0.95], interval: '1d', }) ``` ## 型別 ### `FeedbackRecord` **feedbackId** (`string | null`): 此回饋事件的唯一 ID。省略時,由 server route 產生。 **timestamp** (`Date`): 記錄回饋的時間。 **traceId** (`string | null`): 可用時,作為回饋目標定位依據的 trace。 **spanId** (`string | null`): 可用時,作為回饋目標定位依據的 span。 **feedbackSource** (`string | null`): 選用的來源 metadata,例如 'user'、'qa'、'studio' 或 'system'。 **source** (`string | null`): feedbackSource 的已淘汰 alias。 **feedbackType** (`string`): 回饋類型,例如 'rating'、'thumbs'、'comment' 或 'correction'。 **value** (`number | string`): 回饋值。數值型值支援彙總、細分、時間序列與百分位數查詢。 **comment** (`string | null`): 回饋的其他留言或 context。 **feedbackUserId** (`string | null`): 提供回饋的使用者。 **sourceId** (`string | null`): 此回饋連結之來源記錄的 ID,例如實驗結果 ID。 **metadata** (`Record | null`): 回饋記錄的使用者自訂 metadata。 ### 共用 context 欄位 回饋記錄可包含共用的可觀測性 context 欄位,用於篩選、分組,以及與 trace、log、指標和分數建立關聯。 **entityType** (`EntityType | null`): 產生 signal 的實體類型。 **entityId** (`string | null`): 產生 signal 之實體的 ID。 **entityName** (`string | null`): 產生 signal 之實體的名稱。 **parentEntityType** (`EntityType | null`): 父實體的實體類型。 **parentEntityId** (`string | null`): 父實體的 ID。 **parentEntityName** (`string | null`): 父實體的名稱。 **rootEntityType** (`EntityType | null`): 根實體的實體類型。 **rootEntityId** (`string | null`): 根實體的 ID。 **rootEntityName** (`string | null`): 根實體的名稱。 **userId** (`string | null`): 觸發執行的人類終端使用者。 **organizationId** (`string | null`): 多租戶組織或帳戶。 **resourceId** (`string | null`): 較廣泛的資源 context。 **runId** (`string | null`): 執行 run 識別碼。 **sessionId** (`string | null`): 用於將 trace 分組的 Session 識別碼。 **threadId** (`string | null`): 對話 thread 識別碼。 **requestId** (`string | null`): 用於關聯的 HTTP request ID。 **environment** (`string | null`): 部署環境。 **serviceName** (`string | null`): 服務名稱。 **scope** (`Record | null`): Package、應用程式版本或部署 metadata。 **entityVersionId** (`string | null`): 產生 signal 之實體的版本 ID。 **parentEntityVersionId** (`string | null`): 父實體的版本 ID。 **rootEntityVersionId** (`string | null`): 根實體的版本 ID。 **experimentId** (`string | null`): 實驗或 eval run 識別碼。 **executionSource** (`string | null`): 執行來源,例如本機、cloud 或 CI。 **tags** (`string[] | null`): 用於篩選的標籤。 ### `FeedbackInput` 請搭配 `mastra.observability.addFeedback()`、`recordedTrace.addFeedback()` 與 `recordedSpan.addFeedback()` 使用 `FeedbackInput`。 **feedbackSource** (`string`): 回饋的選用來源 metadata。 **source** (`string`): feedbackSource 的已淘汰 alias。 **feedbackType** (`string`): 要記錄的回饋類型。 **value** (`number | string`): 要記錄的回饋值。 **comment** (`string`): 其他留言或 context。 **feedbackUserId** (`string`): 提供回饋的使用者。 **userId** (`string`): feedbackUserId 的已淘汰 alias。 **metadata** (`Record`): 其他回饋專屬 metadata。 **experimentId** (`string`): 實驗或 eval run 識別碼。 **sourceId** (`string`): 此回饋連結之來源記錄的 ID。 ### `FeedbackFilter` 請在 `listFeedback()` 與 OLAP 查詢的 `filters` 中使用 `FeedbackFilter`。 **timestamp** (`{ start?: Date; end?: Date; startExclusive?: boolean; endExclusive?: boolean }`): 依時間戳記範圍篩選。 **traceId** (`string`): 依 trace ID 篩選。 **spanId** (`string`): 依 span ID 篩選。 **feedbackType** (`string | string[]`): 依一或多個回饋類型篩選。 **feedbackSource** (`string`): 依回饋來源篩選。 **source** (`string`): feedbackSource 的已淘汰 alias。 **feedbackUserId** (`string`): 依提供回饋的使用者篩選。 **entityType** (`EntityType`): 依實體類型篩選。 **entityName** (`string`): 依實體名稱篩選。 **entityVersionId** (`string`): 依實體版本 ID 篩選。 **parentEntityType** (`EntityType`): 依父實體類型篩選。 **parentEntityName** (`string`): 依父實體名稱篩選。 **parentEntityVersionId** (`string`): 依父實體版本 ID 篩選。 **rootEntityType** (`EntityType`): 依根實體類型篩選。 **rootEntityName** (`string`): 依根實體名稱篩選。 **rootEntityVersionId** (`string`): 依根實體版本 ID 篩選。 **userId** (`string`): 依人類終端使用者 ID 篩選。 **organizationId** (`string`): 依組織 ID 篩選。 **resourceId** (`string`): 依資源 ID 篩選。 **runId** (`string`): 依 run ID 篩選。 **sessionId** (`string`): 依 session ID 篩選。 **threadId** (`string`): 依 thread ID 篩選。 **requestId** (`string`): 依 request ID 篩選。 **serviceName** (`string`): 依服務名稱篩選。 **environment** (`string`): 依環境篩選。 **executionSource** (`string`): 依執行來源篩選。 **experimentId** (`string`): 依實驗或 eval run 識別碼篩選。 **tags** (`string[]`): 依標籤篩選。相符記錄必須包含所有指定標籤。 ## HTTP route | 方法 | 路徑 | 用途 | 權限 | | ------ | ----------------------------------------- | ---------- | -------------------- | | `GET` | `/api/observability/feedback` | 列出回饋記錄 | 無衍生權限 | | `POST` | `/api/observability/feedback` | 建立回饋記錄 | 無衍生權限 | | `POST` | `/api/observability/feedback/aggregate` | 傳回單一彙總值 | `observability:read` | | `POST` | `/api/observability/feedback/breakdown` | 依維度將回饋分組 | `observability:read` | | `POST` | `/api/observability/feedback/timeseries` | 依時間間隔將回饋分桶 | `observability:read` | | `POST` | `/api/observability/feedback/percentiles` | 傳回百分位數序列 | `observability:read` | ## 相關內容 - [回饋指南](https://mastra.zisheng.pro/zh-TW/docs/observability/feedback) - [Client SDK 可觀測性參考](https://mastra.zisheng.pro/zh-TW/reference/client-js/observability) - [可觀測性設定](https://mastra.zisheng.pro/zh-TW/reference/observability/tracing/configuration)