| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| import type { SettingsAssistantPreferences } from './generated/runtime.js'; | |
| import type { ComposerPrediction } from './composer-view.js'; | |
| import type { ServiceMethods } from './generated/methods.js'; | |
| type Trigger = 'live' | 'catch_up'; | |
| type ComposerState = 'restoring' | 'occupied' | 'empty'; | |
| type Suggestion = NonNullable<ReturnType<ComposerPrediction['current']>>; | |
| type Result = ServiceMethods['agent']['predictNextMessage']['output']; | |
| type Source = { | |
| readonly turnId: string; | |
| readonly trigger: Trigger; | |
| readonly contextKey: string; | |
| settingsReady: boolean; | |
| state: | |
| | { kind: 'unsettled' } | |
| | { kind: 'settled'; revision: number; value: Suggestion | null } | |
| | { kind: 'skipped' } | |
| | { kind: 'dismissed' }; | |
| }; | |
| export function predictionTrigger(input: { | |
| completedTurnId: string | null; | |
| activeTurnId: string | null; | |
| previousActiveTurnId: string | null; | |
| completedEventTurnId: string | null; | |
| }): Trigger { | |
| return input.completedTurnId !== null && | |
| input.activeTurnId === null && | |
| (input.completedTurnId === input.previousActiveTurnId || | |
| input.completedTurnId === input.completedEventTurnId) | |
| ? 'live' | |
| : 'catch_up'; | |
| } | |
| export type PredictionOptions = { | |
| sessionId: string; | |
| preferences(): Promise<SettingsAssistantPreferences>; | |
| generate( | |
| input: ServiceMethods['agent']['predictNextMessage']['input'], | |
| onCancel: (cancel: () => void) => void, | |
| ): Promise<Result | null>; | |
| changed(value: Suggestion | null): void; | |
| report(reason: unknown): void; | |
| }; | |
| type Request = { | |
| source: Source; | |
| revision: number; | |
| cancelled: boolean; | |
| cancelOperation?: () => void; | |
| }; | |
| type PreferencesState = | |
| | { kind: 'unknown' } | |
| | { kind: 'invalidated'; revision: number } | |
| | { kind: 'ready'; value: SettingsAssistantPreferences }; | |
| export function createPreferencesOwner() { | |
| let state: PreferencesState = { kind: 'unknown' }; | |
| const observers = new Set<() => void>(); | |
| const revision = () => | |
| state.kind === 'unknown' | |
| ? null | |
| : state.kind === 'invalidated' | |
| ? state.revision | |
| : state.value.revision; | |
| const changed = () => { | |
| for (const observe of observers) observe(); | |
| }; | |
| return { | |
| revision, | |
| value: () => (state.kind === 'ready' ? state.value : null), | |
| observe(value: SettingsAssistantPreferences) { | |
| const previous = revision(); | |
| if (previous !== null && value.revision < previous) return; | |
| if (state.kind === 'ready' && state.value === value) return; | |
| state = { kind: 'ready', value }; | |
| changed(); | |
| }, | |
| invalidate(next: number) { | |
| const previous = revision(); | |
| if (previous !== null && next < previous) return; | |
| state = { kind: 'invalidated', revision: next }; | |
| changed(); | |
| }, | |
| subscribe(observe: () => void) { | |
| observers.add(observe); | |
| return () => observers.delete(observe); | |
| }, | |
| }; | |
| } | |
| export function createPredictionController( | |
| options: PredictionOptions, | |
| preferencesOwner = createPreferencesOwner(), | |
| ) { | |
| let alive = true; | |
| let eligible = false; | |
| let composer: ComposerState = 'restoring'; | |
| let source: Source | null = null; | |
| let shown: Suggestion | null = null; | |
| let request: Request | null = null; | |
| let reading: { source: Source; stale: boolean } | null = null; | |
| const publish = (value: Suggestion | null) => { | |
| if (!alive || shown === value) return; | |
| shown = value; | |
| options.changed(value); | |
| }; | |
| const cancel = () => { | |
| if (request && !request.cancelled) { | |
| request.cancelled = true; | |
| request.cancelOperation?.(); | |
| } | |
| }; | |
| const owns = (ticket: Request) => { | |
| const config = preferencesOwner.value(); | |
| return ( | |
| alive && | |
| eligible && | |
| ticket.source.state.kind !== 'dismissed' && | |
| !ticket.cancelled && | |
| source === ticket.source && | |
| config?.revision === ticket.revision && | |
| config.predictions.enabled === true | |
| ); | |
| }; | |
| const generate = async (ticket: Request) => { | |
| try { | |
| if (!owns(ticket)) return; | |
| const result = await options.generate( | |
| { | |
| sessionId: options.sessionId, | |
| expectedTurnId: ticket.source.turnId, | |
| trigger: ticket.source.trigger, | |
| }, | |
| (cancel) => { | |
| if (ticket.cancelled) cancel(); | |
| else ticket.cancelOperation = cancel; | |
| }, | |
| ); | |
| if (!owns(ticket) || !result) return; | |
| if (result.sourceTurnId !== ticket.source.turnId || result.policyRevision !== ticket.revision) | |
| return; | |
| const settled = { | |
| kind: 'settled' as const, | |
| revision: ticket.revision, | |
| value: | |
| result.suggestion == null | |
| ? null | |
| : { sourceTurnId: result.sourceTurnId, text: result.suggestion }, | |
| }; | |
| ticket.source.state = settled; | |
| publish(settled.value); | |
| } catch (reason) { | |
| if (!ticket.cancelled) options.report(reason); | |
| } finally { | |
| if (request === ticket) { | |
| if ( | |
| !ticket.cancelled && | |
| source === ticket.source && | |
| (ticket.source.state.kind !== 'settled' || | |
| ticket.source.state.revision !== ticket.revision) | |
| ) | |
| ticket.source.state = { | |
| kind: 'settled', | |
| revision: ticket.revision, | |
| value: null, | |
| }; | |
| request = null; | |
| refresh(); | |
| } | |
| } | |
| }; | |
| const readPreferences = () => { | |
| if (reading || !source || !alive || !eligible) return; | |
| const ticket = { source, stale: false }; | |
| reading = ticket; | |
| void options | |
| .preferences() | |
| .then( | |
| (value) => { | |
| if (!alive || source !== ticket.source) return; | |
| const revision = preferencesOwner.revision(); | |
| ticket.source.settingsReady = revision === null || value.revision >= revision; | |
| ticket.stale = !ticket.source.settingsReady; | |
| preferencesOwner.observe(value); | |
| refresh(); | |
| }, | |
| (reason) => { | |
| if (alive && source === ticket.source) options.report(reason); | |
| }, | |
| ) | |
| .finally(() => { | |
| if (reading === ticket) { | |
| reading = null; | |
| if (alive && (source !== ticket.source || ticket.stale)) refresh(); | |
| } | |
| }); | |
| }; | |
| function refresh() { | |
| if (!alive) return; | |
| if ( | |
| source && | |
| composer === 'occupied' && | |
| (request?.source !== source || request.cancelled) && | |
| (source.state.kind === 'unsettled' || | |
| (source.state.kind === 'settled' && | |
| preferencesOwner.revision() !== null && | |
| source.state.revision !== preferencesOwner.revision())) | |
| ) | |
| source.state = { kind: 'skipped' }; | |
| if ( | |
| !eligible || | |
| !source || | |
| source.state.kind === 'dismissed' || | |
| source.state.kind === 'skipped' | |
| ) { | |
| publish(null); | |
| cancel(); | |
| return; | |
| } | |
| const config = preferencesOwner.value(); | |
| if (!source.settingsReady || !config) { | |
| publish(null); | |
| cancel(); | |
| readPreferences(); | |
| return; | |
| } | |
| if (!config.predictions.enabled) { | |
| publish(null); | |
| cancel(); | |
| return; | |
| } | |
| if (source.state.kind === 'settled' && source.state.revision === config.revision) { | |
| publish(source.state.value); | |
| return; | |
| } | |
| publish(null); | |
| if (request) { | |
| if (request.source !== source || request.revision !== config.revision) cancel(); | |
| return; | |
| } | |
| if (composer !== 'empty') return; | |
| const ticket: Request = { | |
| source, | |
| revision: config.revision, | |
| cancelled: false, | |
| }; | |
| request = ticket; | |
| void generate(ticket); | |
| } | |
| const changedPreferences = () => { | |
| if (source && !preferencesOwner.value()) source.settingsReady = false; | |
| refresh(); | |
| }; | |
| const unsubscribe = preferencesOwner.subscribe(changedPreferences); | |
| const presentation: ComposerPrediction = { | |
| current: () => shown, | |
| dismiss(sourceTurnId) { | |
| if (source?.turnId !== sourceTurnId) return; | |
| source.state = { kind: 'dismissed' }; | |
| refresh(); | |
| }, | |
| }; | |
| return { | |
| presentation, | |
| source(turnId: string | null, trigger: Trigger, contextKey = '') { | |
| if ( | |
| !alive || | |
| ((source?.turnId ?? null) === turnId && | |
| (source === null || source.contextKey === contextKey)) | |
| ) | |
| return; | |
| source = | |
| turnId === null | |
| ? null | |
| : { | |
| turnId, | |
| trigger, | |
| contextKey, | |
| settingsReady: false, | |
| state: { kind: 'unsettled' }, | |
| }; | |
| refresh(); | |
| }, | |
| eligible(value: boolean, nextComposer: ComposerState) { | |
| if (!alive || (eligible === value && composer === nextComposer)) return; | |
| eligible = value; | |
| composer = nextComposer; | |
| if (!value && source) source.settingsReady = false; | |
| refresh(); | |
| }, | |
| dispose() { | |
| if (!alive) return; | |
| cancel(); | |
| unsubscribe(); | |
| alive = false; | |
| shown = null; | |
| }, | |
| }; | |
| } |