Skip to content
15 changes: 13 additions & 2 deletions apps/website/src/components/workshop/WorkflowPlayground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
workflowErrorKey,
workflowStatusKey
} from '../../config/workshop-workflow-presentation'
import { panelSaysRefusal } from '../../lib/workshop/workflow-refusal'
import { useTablist } from '../../composables/useTablist'
import { useWorkflowFormDraft } from '../../composables/useWorkflowFormDraft'
import { useWorkflowRun } from '../../composables/useWorkflowRun'
Expand Down Expand Up @@ -125,6 +126,12 @@ const error = computed(() =>
: undefined
)
const fieldErrors = computed(() => error.value?.fieldErrors ?? {})
// What is left for this page to say is what the panel does not carry.
const refusalSaidHere = computed(() =>
error.value && !panelSaysRefusal(state.value)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
? t(workflowErrorKey(error.value))
: undefined
)
const statusLabel = computed(() => {
if (cancelRequested.value && busy.value)
return t('workshop.workflow.cancelling')
Expand Down Expand Up @@ -251,8 +258,12 @@ function start() {
>
{{ t('workshop.workflow.paused') }}
</p>
<p v-if="error" role="alert" class="text-sm text-primary-comfy-red">
{{ t(workflowErrorKey(error)) }}
<p
v-if="refusalSaidHere"
role="alert"
class="text-sm text-primary-comfy-red"
>
{{ refusalSaidHere }}
</p>
<WorkflowRunControls
:state="state"
Expand Down
63 changes: 62 additions & 1 deletion apps/website/src/components/workshop/WorkflowResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/vue'
import { assert, describe, expect, it, onTestFinished, vi } from 'vitest'
import { defineComponent, h, shallowRef } from 'vue'

import { createWorkflowApi } from '../../config/workshop-workflow-api'
import {
createWorkflowApi,
WorkshopWorkflowError
} from '../../config/workshop-workflow-api'
import { subscribeToWorkshopBuyCredits } from '../../config/workshop-buy-credits'
import { workflowDetailsBySlug } from '../../config/workshop-workflow-content'
import { createWorkflowController } from '../../config/workshop-workflow-controller'
import type { WorkflowState } from '../../config/workshop-workflow-state'
Expand Down Expand Up @@ -196,3 +200,60 @@ describe('WorkflowResults', () => {
expect(f.state.value.phase).toBe('settled')
})
})

// A request Cloud turns down used to leave the panel showing the example: a
// picture of a successful run, beside a form that had just been refused.
describe('a refused request', () => {
function mountRefused(code: WorkshopWorkflowError['code']) {
const model = workflowDetailsBySlug.get('workflows/remove-background')
assert(model)
render(WorkflowResults, {
props: {
model,
state: {
phase: 'failed',
error: new WorkshopWorkflowError(code)
} satisfies WorkflowState,
exampleIndex: 0,
busy: false,
statusLabel: '',
canStart: true,
refreshOutput: async () => undefined
}
})
}

it('stands the refusal up in the output panel instead of the example', () => {
mountRefused('insufficient_credits')

// Shown and also announced, so the sentence is on the page more than once.
expect(
screen.getAllByText('Not enough credits. Add credits to continue.').length
).toBeGreaterThan(0)
expect(screen.queryByText('An example from this template.')).toBeNull()
})

// The panel already knows how to send a reader to buy credits; it was never
// being told that was the refusal. A button that reaches nothing is not a way
// out, so press it and watch for the request the page answers.
it('offers the way out the refusal has', async () => {
const asked = vi.fn()
onTestFinished(subscribeToWorkshopBuyCredits(asked))
mountRefused('insufficient_credits')

await userEvent
.setup()
.click(screen.getByRole('button', { name: /credits/i }))
expect(asked).toHaveBeenCalledOnce()
})

// A refusal the panel has no sentence for is said beside the form instead.
// The panel then shows nothing — not the example, which is a picture of a run
// that worked.
it('shows nothing rather than the example for a refusal it cannot word', () => {
mountRefused('access_denied')

expect(screen.queryByText('An example from this template.')).toBeNull()
expect(screen.getByText('Your output will appear here.')).toBeTruthy()
})
})
49 changes: 38 additions & 11 deletions apps/website/src/components/workshop/WorkflowResults.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useWorkshopDelivery } from '../../composables/useWorkshopDelivery'
import type { WorkflowState } from '../../config/workshop-workflow-state'
import { workflowOutputs } from '../../config/workshop-workflow-response'
import { outputLabels } from '../../lib/workshop/output-labels'
import { requestWorkshopBuyCredits } from '../../config/workshop-buy-credits'
import { workflowRunFailure } from '../../lib/workshop/workflow-refusal'
import { t } from '../../i18n/translations'
import { captureWorkshopEvent } from '../../scripts/posthog'
import type { WorkshopRunAnalytics } from '../../scripts/workshop-analytics'
Expand Down Expand Up @@ -53,19 +55,43 @@ const runningState = computed<RunState>(() => ({
const outputs = computed(() =>
observation.value ? workflowOutputs(observation.value) : []
)
const outputState = computed<RunState>(() => {
// What Cloud last said about a run it accepted. Undefined while it has said
// nothing the panel can draw yet, including a finished run whose outputs have
// not arrived.
const observedState = computed<RunState | undefined>(() => {
const result = observation.value
if (result?.run.state === 'succeeded' && outputs.value[0]) {
return {
status: 'succeeded',
output: outputs.value[0],
nsfw: false,
completedAt: Date.parse(result.run.completedAt ?? result.run.updatedAt)
}
}
if (result?.run.state === 'cancelled') return { status: 'idle' }
if (result?.run.state === 'failed')
if (!result) return undefined
const output = outputs.value[0]
if (result.run.state === 'succeeded')
return output
? {
status: 'succeeded',
output,
nsfw: false,
completedAt: Date.parse(
result.run.completedAt ?? result.run.updatedAt
)
}
: undefined
if (result.run.state === 'cancelled') return { status: 'idle' }
if (result.run.state === 'failed')
return { status: 'failed', reason: 'provider', fieldErrors: {} }
return undefined
})
// A request Cloud turned down used to fall through to the example, so the panel
// showed what the workflow makes while the run had just been refused. Where the
// panel has the words for it, it says so; where it does not, the page says it
// beside the form and the panel shows nothing, because nothing was made.
const refusedState = computed<RunState | undefined>(() => {
if (state.phase !== 'failed') return undefined
const reason = workflowRunFailure(state.error)
return reason
? { status: 'failed', reason, fieldErrors: state.error.fieldErrors }
: { status: 'idle' }
})
const outputState = computed<RunState>(() => {
if (observedState.value) return observedState.value
if (refusedState.value) return refusedState.value
if (busy) return runningState.value
if (state.phase === 'settled') return { status: 'idle' }
return exampleState.value
Expand Down Expand Up @@ -185,6 +211,7 @@ function captureDownload(kind: RunOutput['kind']) {
:retry-disabled="!canStart"
refreshable
@retry="emit('retry')"
@buy-credits="requestWorkshopBuyCredits"
@refresh="refreshUrl"
@delivery="onDelivery"
@playback-started="delivery.beginPlayback"
Expand Down
154 changes: 154 additions & 0 deletions apps/website/src/lib/workshop/workflow-refusal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { describe, expect, it } from 'vitest'

import { WorkshopWorkflowError } from '../../config/workshop-workflow-api'
import type { RunFailure } from '../../config/workshop-run'
import type {
WorkflowErrorCode,
WorkflowRun,
WorkflowRunSummary
} from '../../config/workshop-workflow-response'
import type { WorkflowState } from '../../config/workshop-workflow-state'
import type { SavedWorkflow } from '../../config/workshop-workflow-storage'
import { failureLabelKey } from './failure-label'
import { panelSaysRefusal, workflowRunFailure } from './workflow-refusal'

// Every code the API can report and the refusal the panel stands up for it. The
// table is the specification: a code whose event the panel has no sentence for
// reads as undefined here, rather than as a sentence about something else.
const REFUSALS: readonly [
WorkflowErrorCode | 'network' | 'response' | 'persistence',
RunFailure | undefined
][] = [
['insufficient_credits', 'noCredits'],
['rate_limited', 'rateLimit'],
['network', 'network'],
['response', 'response'],
['execution_failed', 'provider'],
// What is wrong with an input belongs beside the input, and the panel's own
// words for a rejection it cannot pin to a field are about a model.
['invalid_request', undefined],
['invalid_input', undefined],
['payload_too_large', undefined],
['unsupported_media_type', undefined],
// A session that expired and a form gone out of date the panel has no word
// for at all.
['not_authenticated', undefined],
['workflow_not_found', undefined],
['definition_changed', undefined],
['definition_incompatible', undefined],
// Codes the panel has a word for, about a different event: a workspace Cloud
// denied is not a provider's content policy; an input upload that never
// arrived is not a model being down; outputs that could not be fetched after
// a run finished is not a connection that dropped mid-run; browser storage
// the page could not write names its own fix, which the panel's sentence
// does not.
['access_denied', undefined],
['media_unavailable', undefined],
['delivery_failed', undefined],
['submission_unknown', undefined],
['persistence', undefined],
['run_not_found', undefined]
]

describe('workflowRunFailure', () => {
it.for(REFUSALS)('stands %s up as %s', ([code, refusal]) => {
expect(workflowRunFailure({ code })).toBe(refusal)
})

// A code this page has not met yet is not given a sentence about something
// else; the page keeps saying what it says today. 'toString' is here because
// a plain object would have answered it with a method of its prototype.
it.for(['teapot', 'toString', 'constructor'] as const)(
'claims nothing about %s, a code it does not know',
(code) => {
expect(
workflowRunFailure({ code: code as WorkflowErrorCode })
).toBeUndefined()
}
)

it('only ever names a refusal the panel has words for', () => {
for (const [, refusal] of REFUSALS)
if (refusal) expect(failureLabelKey[refusal]).toBeTruthy()
})
})

// Whether the page keeps quiet beside the form follows from the same table: it
// speaks exactly where the panel does not, so the reader is never handed the
// same refusal twice, nor two different accounts of it.
describe('panelSaysRefusal', () => {
it.for(REFUSALS)('leaves %s to the panel only as %s', ([code, refusal]) => {
expect(
panelSaysRefusal({
phase: 'failed',
error: new WorkshopWorkflowError(code)
})
).toBe(refusal !== undefined)
})

it.for([{ phase: 'idle' }, { phase: 'preparing' }] as const)(
'says nothing about a $phase page',
(state) => {
expect(panelSaysRefusal(state)).toBe(false)
}
)

// A run Cloud accepted and then reported failed is the panel's to say, and it
// says it as a provider failure. The page used to add a second sentence of its
// own here — in different words, telling the reader to change the inputs the
// panel had just told them to leave alone.
it.for([
{ reported: 'failed', panel: true },
{ reported: 'succeeded', panel: false },
{ reported: 'cancelled', panel: false }
] as const)(
'leaves a run reported $reported to the panel: $panel',
({ reported, panel }) => {
expect(panelSaysRefusal(observed(reported))).toBe(panel)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
)

// A dropped connection is the one state that carries both: what Cloud last
// said about the run, and the error that dropped it. The run wins. Reading the
// error first would hand this back to the page, which has a second sentence
// ready for a code the panel cannot name.
it('leaves a failed run to the panel when the connection dropped on a code it has no words for', () => {
expect(
panelSaysRefusal({
phase: 'interrupted',
record: RECORD,
observation: run('failed'),
error: new WorkshopWorkflowError('media_unavailable')
})
).toBe(true)
})
})

const RECORD: SavedWorkflow = {
version: 2,
cancelRequested: false,
stage: 'run',
runId: 'run',
workflowId: 'workflow',
definitionVersion: '1'
}

function run(state: WorkflowRunSummary['state']): WorkflowRun {
const stamp = new Date(0).toISOString()
return {
run: {
id: 'run',
workflowId: 'workflow',
definitionVersion: '1',
state,
outputState: 'ready',
createdAt: stamp,
updatedAt: stamp
},
outputs: []
}
}

function observed(state: WorkflowRunSummary['state']): WorkflowState {
return { phase: 'settled', record: RECORD, observation: run(state) }
}
58 changes: 58 additions & 0 deletions apps/website/src/lib/workshop/workflow-refusal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { WorkshopWorkflowError } from '../../config/workshop-workflow-api'
import type { RunFailure } from '../../config/workshop-run'
import type { WorkflowState } from '../../config/workshop-workflow-state'

/**
* Which refusal the output panel should stand up, where the panel's own words
* are about the same event as this page's.
*
* The two halves of the Hub refuse things in different vocabularies, and a
* shared word is not a shared meaning: Cloud denying a workspace is not a
* provider's content policy, and an input upload that never arrived is not a
* model being down. So a code reaches the panel only where the sentence the
* panel already has says what actually happened; everywhere else the page's
* own accurate sentence stays beside the form, and the panel shows nothing
* rather than either the wrong words or a picture of a run that succeeded.
*/
export function workflowRunFailure(
error: Pick<WorkshopWorkflowError, 'code'>
): RunFailure | undefined {
return REFUSALS.get(error.code)
}

// A Map, not an object: a code the page has never met must read as absent, and
// an object would hand back whatever Object.prototype happens to call by that
// name — 'toString' would arrive as a refusal.
const REFUSALS = new Map<string, RunFailure>([
// The same sentence in both, and the only way out of it — Add credits, or
// the reader's own workspace — is the panel's to offer.
['insufficient_credits', 'noCredits'],
// The same sentence in both.
['rate_limited', 'rateLimit'],
// Both about a connection that went while a run may be alive and billed.
['network', 'network'],
// A response that arrived and could not be read, which is what this page
// otherwise reports only as a run it could not check.
['response', 'response'],
// One event, and the panel says more of it: the credits that may have gone
// without a result, and the request ID to quote.
['execution_failed', 'provider']
])

/**
* Whether the output panel is the one saying this failure. The page then keeps
* quiet beside the form rather than handing the reader the same thing twice —
* and twice in different words wherever the two vocabularies disagree.
*
* A run Cloud reports as failed is always the panel's: it stands that up as a
* provider failure regardless of what the page made of the request. Only a
* request Cloud turned down depends on whether the panel has words for the
* code.
*/
export function panelSaysRefusal(state: WorkflowState): boolean {
if ('observation' in state && state.observation?.run.state === 'failed')
return true
return (
state.phase === 'failed' && workflowRunFailure(state.error) !== undefined
)
}
Loading