|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Event Stream Adapter Custom Element</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + <script type="module"> |
| 9 | + import { customElement } from '/assets/custom-element/custom-element.js'; |
| 10 | + import { getActivityLivestreamingMetadata } from 'botframework-webchat-core'; |
| 11 | + import { EventSourceParserStream } from 'eventsource-parser/stream'; |
| 12 | + import createStreamCoalescer from '/assets/esm/adapter/createStreamCoalescer.js'; |
| 13 | + import { forIterator } from '/assets/esm/adapter/demuxChainOfThought.js'; |
| 14 | + |
| 15 | + function createObservable(subscribe) { |
| 16 | + return Object.freeze({ subscribe }); |
| 17 | + } |
| 18 | + |
| 19 | + customElement('event-stream-adapter', currentDocument => |
| 20 | + class EventStreamAdapterElement extends HTMLElement { |
| 21 | + static get observedAttributes() { return ['compat', 'href']; } |
| 22 | + |
| 23 | + #abortController = null; |
| 24 | + #activities = []; |
| 25 | + #activityBuffer = []; |
| 26 | + #activityObservers = []; |
| 27 | + #directLine; |
| 28 | + |
| 29 | + constructor() { |
| 30 | + super(); |
| 31 | + |
| 32 | + const self = this; |
| 33 | + |
| 34 | + this.#directLine = Object.freeze({ |
| 35 | + activity$: createObservable(observerOrNext => { |
| 36 | + const observer = typeof observerOrNext === 'function' |
| 37 | + ? { next: observerOrNext } |
| 38 | + : observerOrNext; |
| 39 | + |
| 40 | + for (const activity of self.#activityBuffer) { |
| 41 | + observer.next(activity); |
| 42 | + } |
| 43 | + |
| 44 | + self.#activityObservers.push(observer); |
| 45 | + |
| 46 | + return Object.freeze({ |
| 47 | + unsubscribe() { |
| 48 | + const i = self.#activityObservers.indexOf(observer); |
| 49 | + i !== -1 && self.#activityObservers.splice(i, 1); |
| 50 | + } |
| 51 | + }); |
| 52 | + }), |
| 53 | + |
| 54 | + connectionStatus$: createObservable(observerOrNext => { |
| 55 | + const observer = typeof observerOrNext === 'function' |
| 56 | + ? { next: observerOrNext } |
| 57 | + : observerOrNext; |
| 58 | + |
| 59 | + observer.next(0); |
| 60 | + observer.next(1); |
| 61 | + observer.next(2); |
| 62 | + |
| 63 | + return Object.freeze({ unsubscribe() {} }); |
| 64 | + }), |
| 65 | + |
| 66 | + end() {}, |
| 67 | + |
| 68 | + postActivity() { |
| 69 | + return createObservable(observerOrNext => { |
| 70 | + const observer = typeof observerOrNext === 'function' |
| 71 | + ? { next: observerOrNext } |
| 72 | + : observerOrNext; |
| 73 | + |
| 74 | + observer.next(crypto.randomUUID()); |
| 75 | + observer.complete?.(); |
| 76 | + |
| 77 | + return Object.freeze({ unsubscribe() {} }); |
| 78 | + }); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + get compat() { |
| 84 | + return this.getAttribute('compat') || 'webchat'; |
| 85 | + } |
| 86 | + |
| 87 | + get directLine() { |
| 88 | + return this.#directLine; |
| 89 | + } |
| 90 | + |
| 91 | + connectedCallback() { |
| 92 | + this.#load(); |
| 93 | + } |
| 94 | + |
| 95 | + disconnectedCallback() { |
| 96 | + this.#abortController?.abort(); |
| 97 | + this.#abortController = null; |
| 98 | + } |
| 99 | + |
| 100 | + #emitToAdapter(activity) { |
| 101 | + this.#activityBuffer.push(activity); |
| 102 | + |
| 103 | + for (const observer of this.#activityObservers) { |
| 104 | + observer.next(activity); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + #emitEvent(activity) { |
| 109 | + this.#activities.push(activity); |
| 110 | + |
| 111 | + const meta = getActivityLivestreamingMetadata(activity); |
| 112 | + const allActivities = this.#activities; |
| 113 | + const detail = { activity }; |
| 114 | + |
| 115 | + meta && Object.defineProperty(detail, 'activities', { |
| 116 | + enumerable: true, |
| 117 | + get: () => allActivities.values() |
| 118 | + }); |
| 119 | + |
| 120 | + this.dispatchEvent(new CustomEvent('activity', { |
| 121 | + bubbles: true, |
| 122 | + detail: Object.freeze(detail) |
| 123 | + })); |
| 124 | + } |
| 125 | + |
| 126 | + async #load() { |
| 127 | + const href = this.getAttribute('href'); |
| 128 | + |
| 129 | + if (!href) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + this.#abortController?.abort(); |
| 134 | + this.#abortController = new AbortController(); |
| 135 | + |
| 136 | + const { signal } = this.#abortController; |
| 137 | + const compat = this.compat; |
| 138 | + const useAdapter = compat === 'webchat' || compat === 'both'; |
| 139 | + const useEvents = compat === 'events' || compat === 'both'; |
| 140 | + |
| 141 | + try { |
| 142 | + for await (const activity of forIterator({}, this.#fetchStreamed(href, signal))) { |
| 143 | + if (signal.aborted) { |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + useAdapter && this.#emitToAdapter(activity); |
| 148 | + useEvents && this.#emitEvent(activity); |
| 149 | + } |
| 150 | + } catch (error) { |
| 151 | + signal.aborted || console.error('event-stream-adapter:', error); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + async *#fetchStreamed(href, signal) { |
| 156 | + const typingMap = new Map(); |
| 157 | + const res = await fetch(new URL(href, location.href), { signal }); |
| 158 | + |
| 159 | + yield* res.body |
| 160 | + .pipeThrough(new TextDecoderStream()) |
| 161 | + .pipeThrough(new EventSourceParserStream()) |
| 162 | + .pipeThrough( |
| 163 | + new TransformStream({ |
| 164 | + transform({ data, event }, controller) { |
| 165 | + if (event === 'end') { |
| 166 | + controller.terminate(); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + if (event !== 'activity') { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + const activity = JSON.parse(data); |
| 175 | + |
| 176 | + if ( |
| 177 | + activity.type === 'typing' && |
| 178 | + activity.text && |
| 179 | + activity.channelData?.streamType === 'streaming' |
| 180 | + ) { |
| 181 | + const streamId = activity.channelData?.streamId || activity.id; |
| 182 | + let accumulated = typingMap.get(streamId) || ''; |
| 183 | + |
| 184 | + if (activity.channelData?.chunkType === 'delta') { |
| 185 | + accumulated += activity.text; |
| 186 | + activity.text = accumulated; |
| 187 | + } else { |
| 188 | + accumulated = activity.text; |
| 189 | + } |
| 190 | + |
| 191 | + typingMap.set(streamId, accumulated); |
| 192 | + } |
| 193 | + |
| 194 | + controller.enqueue(activity); |
| 195 | + } |
| 196 | + }) |
| 197 | + ) |
| 198 | + .pipeThrough(createStreamCoalescer()); |
| 199 | + } |
| 200 | + } |
| 201 | + ); |
| 202 | + </script> |
| 203 | +</body> |
| 204 | +</html> |
0 commit comments