LCOV - code coverage report
Current view: top level - src/workbench/extensions/agent/crdt - rejectedOpNotice.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 10.0 % 70 7
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { throttle } from 'es-toolkit'
       2              : import type { ThrottledFunction } from 'es-toolkit'
       3              : import type { Op } from '@comfyorg/comfy-multi-player'
       4              : 
       5              : import { useToast } from '@/components/ui/toast/toastStore'
       6              : import { i18n } from '@/i18n'
       7              : import { createToastId } from '@/types/toastId'
       8              : 
       9              : import type { DocOpFailure } from './docFrameClient'
      10              : import { createReportBudget } from './opSender'
      11              : import type { OpsResultView } from './opSender'
      12              : import { reportLostEdit } from './reportLostEdit'
      13              : 
      14            1 : const NOTICE_LIFE_MS = 10_000
      15            1 : const NOTICE_ID = createToastId()
      16              : 
      17              : /** Acknowledged ops the document does not show as written. */
      18              : export interface LostEdits {
      19              :   overwritten: readonly Op[]
      20              :   unconfirmed: readonly Op[]
      21              : }
      22              : 
      23            0 : function rejectedOp(ops: readonly Op[], failed: DocOpFailure): Op | undefined {
      24            0 :   return ops.find((op) => op.op_id === failed.op_id) ?? ops[failed.index]
      25              : }
      26              : 
      27              : /**
      28              :  * Doc-host codes for a refused widget write. Both halves are load-bearing: the
      29              :  * applier raises `uncatalogued_widget_write` and `unknown_widget` for
      30              :  * `add_node` too, so the op the host NAMED must also be a widget write; and a
      31              :  * `set_widget` refused for an unrelated reason (`catalog_required`,
      32              :  * `malformed_op`) is not a widget-addressing failure and gets the generic copy.
      33              :  */
      34            1 : const WIDGET_WRITE_REJECTION_CODES = new Set([
      35              :   'opaque_widgets',
      36              :   'uncatalogued_widget_write',
      37              :   'unknown_widget',
      38              :   'widget_out_of_range'
      39              : ])
      40              : 
      41            0 : function isRejectedWidgetWrite(
      42              :   ops: readonly Op[],
      43              :   failed: DocOpFailure | undefined
      44              : ): boolean {
      45            0 :   if (failed === undefined) return false
      46            0 :   return (
      47              :     WIDGET_WRITE_REJECTION_CODES.has(failed.code) &&
      48              :     rejectedOp(ops, failed)?.op === 'set_widget'
      49              :   )
      50              : }
      51              : 
      52            0 : function nodeFieldTarget(nodeId: string | number, field: string): string {
      53            0 :   const t = i18n.global.t
      54              :   const plain = { escapeParameter: false }
      55            0 :   return t(
      56              :     'agent.editRejected.targets.nodeField',
      57              :     { id: String(nodeId), field },
      58              :     plain
      59              :   )
      60              : }
      61              : 
      62            0 : function rejectedTarget(op: Op | undefined): string | undefined {
      63            0 :   const t = i18n.global.t
      64            0 :   const plain = { escapeParameter: false }
      65            0 :   switch (op?.op) {
      66              :     case 'set_widget':
      67            0 :       return nodeFieldTarget(op.node_id, op.widget)
      68              :     case 'add_node':
      69            0 :       return op.class_type
      70              :     case 'set_node_field':
      71            0 :       return nodeFieldTarget(op.node_id, op.field)
      72              :     case 'delete_node':
      73            0 :       return t(
      74              :         'agent.editRejected.targets.node',
      75              :         { id: String(op.node_id) },
      76              :         plain
      77              :       )
      78              :     case 'clear':
      79            0 :       return t('agent.editRejected.targets.clear')
      80              :     case 'connect':
      81              :     case 'disconnect':
      82            0 :       return t(
      83              :         'agent.editRejected.targets.link',
      84              :         { id: String(op.link_id) },
      85              :         plain
      86              :       )
      87              :     default:
      88            0 :       return undefined
      89              :   }
      90              : }
      91              : 
      92            0 : function rejectedOpContext(op: Op | undefined): {
      93              :   nodeId?: string | number
      94              :   classType?: string
      95              : } {
      96            0 :   switch (op?.op) {
      97              :     case 'set_widget':
      98              :     case 'set_node_field':
      99              :     case 'delete_node':
     100            0 :       return { nodeId: op.node_id }
     101              :     case 'add_node':
     102            0 :       return { nodeId: op.node_id, classType: op.class_type }
     103              :     default:
     104            0 :       return {}
     105              :   }
     106              : }
     107              : 
     108            0 : function withReason(
     109              :   notice: string,
     110              :   target: string | undefined,
     111              :   code: string
     112              : ): string {
     113            0 :   const plain = { escapeParameter: false }
     114            0 :   if (target && code && code !== 'unspecified')
     115            0 :     return i18n.global.t(
     116              :       'agent.editRejected.withReason',
     117              :       { notice, target, code },
     118              :       plain
     119              :     )
     120            0 :   if (target)
     121            0 :     return i18n.global.t(
     122              :       'agent.editRejected.withReasonTarget',
     123              :       { notice, target },
     124              :       plain
     125              :     )
     126            0 :   if (!code || code === 'unspecified') return notice
     127            0 :   return i18n.global.t(
     128              :     'agent.editRejected.withReasonCode',
     129              :     { notice, code },
     130              :     plain
     131              :   )
     132              : }
     133              : 
     134              : /**
     135              :  * Schema §4 aborts the REMAINDER of a batch, so a rejected result can still
     136              :  * carry an applied prefix — which is what `partial` distinguishes.
     137              :  */
     138            0 : function noticeKey(widgetWrite: boolean, partial: boolean): string {
     139            0 :   if (widgetWrite) {
     140            0 :     return partial
     141              :       ? 'agent.editRejected.widgetWritePartial'
     142              :       : 'agent.editRejected.widgetWrite'
     143              :   }
     144            0 :   return partial
     145              :     ? 'agent.editRejected.genericPartial'
     146              :     : 'agent.editRejected.generic'
     147              : }
     148              : 
     149            1 : export function createRejectedOpNotifier() {
     150            1 :   const throttledToasts = new Map<
     151              :     string,
     152              :     ThrottledFunction<(description: string) => void>
     153              :   >()
     154              : 
     155            0 :   function showToast(kind: string, description: string): void {
     156            0 :     const toast = throttledToasts.get(kind)
     157            0 :     if (toast) return toast(description)
     158            0 :     const throttled = throttle(
     159              :       (detail: string) =>
     160              :         useToast().error(i18n.global.t('g.error'), {
     161              :           id: NOTICE_ID,
     162              :           description: detail,
     163              :           duration: NOTICE_LIFE_MS
     164              :         }),
     165              :       NOTICE_LIFE_MS,
     166              :       { edges: ['leading'] }
     167              :     )
     168            0 :     throttledToasts.set(kind, throttled)
     169            0 :     throttled(description)
     170              :   }
     171              : 
     172            0 :   function reportRejectedEdit(
     173              :     ops: readonly Op[],
     174              :     result: OpsResultView,
     175              :     failed: DocOpFailure | undefined,
     176              :     code: string,
     177              :     widgetWrite: boolean
     178              :   ): void {
     179            0 :     const accepted = new Set([...result.applied, ...result.skipped])
     180            0 :     reportLostEdit(
     181              :       'host_rejected',
     182              :       ops.filter((op) => !accepted.has(op.op_id)),
     183              :       {
     184              :         code,
     185              :         workflowId: result.workflowId,
     186              :         message: failed?.message,
     187              :         failedIndex: failed?.index,
     188              :         errorType: widgetWrite
     189              :           ? 'error_applying_agent_widget_edit'
     190              :           : 'error_applying_agent_graph_edit',
     191              :         ...rejectedOpContext(failed && rejectedOp(ops, failed))
     192              :       }
     193              :     )
     194              :   }
     195              : 
     196            0 :   function notifyRejected(ops: readonly Op[], result: OpsResultView): void {
     197            0 :     const { failed } = result
     198            0 :     const widgetWrite = isRejectedWidgetWrite(ops, failed)
     199            0 :     const code = failed?.code ?? result.code ?? 'unspecified'
     200            0 :     reportRejectedEdit(ops, result, failed, code, widgetWrite)
     201              :     const partial = result.applied.length > 0 && failed !== undefined
     202            0 :     const kind = noticeKey(widgetWrite, partial)
     203              :     const notice = i18n.global.t(kind)
     204              :     const target = failed && rejectedTarget(rejectedOp(ops, failed))
     205            0 :     showToast(kind, withReason(notice, target, code))
     206              :   }
     207              : 
     208            1 :   const mayReportLostEdit = createReportBudget()
     209              : 
     210            0 :   function notifyLostEdits(
     211              :     result: OpsResultView,
     212              :     { overwritten, unconfirmed }: LostEdits
     213              :   ): void {
     214            0 :     if (overwritten.length > 0)
     215            0 :       reportLostEdit(
     216              :         'host_rejected',
     217              :         overwritten,
     218              :         {
     219              :           code: 'lww_dropped',
     220              :           workflowId: result.workflowId,
     221              :           errorType: 'error_applying_agent_graph_edit'
     222              :         },
     223              :         { shouldReport: mayReportLostEdit() }
     224              :       )
     225            0 :     if (unconfirmed.length > 0)
     226            0 :       reportLostEdit(
     227              :         'unconfirmed',
     228              :         unconfirmed,
     229              :         { code: 'replayed_without_outcome', workflowId: result.workflowId },
     230              :         { shouldReport: mayReportLostEdit() }
     231              :       )
     232            0 :     if (!result.ok) return
     233            0 :     const kind =
     234              :       overwritten.length > 0
     235              :         ? 'agent.editRejected.overwritten'
     236              :         : 'agent.editRejected.unconfirmed'
     237            0 :     showToast(kind, i18n.global.t(kind))
     238              :   }
     239              : 
     240            1 :   return {
     241              :     notify(ops: readonly Op[], result: OpsResultView) {
     242            0 :       if (result.ok) return
     243            0 :       notifyRejected(ops, result)
     244              :     },
     245              :     notifyLostEdits,
     246              :     notifyWidgetWriteRefused(nodeId: string | number, widgetName: string) {
     247              :       const notice = i18n.global.t('agent.editRejected.widgetWrite')
     248            0 :       showToast(
     249              :         'agent.editRejected.widgetWrite',
     250              :         withReason(notice, nodeFieldTarget(nodeId, widgetName), 'unspecified')
     251              :       )
     252              :     }
     253              :   }
     254              : }
        

Generated by: LCOV version 2.0-1