MCPcopy Index your code
hub / github.com/TanStack/ai / generate

Method generate

packages/ai-client/src/generation-client.ts:160–231  ·  view source on GitHub ↗

* Trigger a generation request. * Only one generation can be in-flight at a time; calling generate() * while already generating will be a no-op.

(input: TInput)

Source from the content-addressed store, hash-verified

158 * while already generating will be a no-op.
159 */
160 async generate(input: TInput): Promise<void> {
161 this.mountDevtools()
162 if (this.isLoading) return
163
164 this.input = input
165 this.progress = null
166 const runId = this.devtoolsBridge.beginRun(input)
167 this.setIsLoading(true)
168 this.setStatus('generating')
169 this.setError(undefined)
170
171 const abortController = new AbortController()
172 this.abortController = abortController
173 const { signal } = abortController
174
175 try {
176 if (this.fetcher) {
177 // Direct fetch path
178 const result = await this.fetcher(input, { signal })
179 if (signal.aborted) return
180 if (result instanceof Response) {
181 // Server function returned SSE Response — parse stream
182 await this.processStream(parseSSEResponse(result, signal), runId)
183 } else {
184 this.devtoolsBridge.ensureRunStarted(runId)
185 this.setResult(result)
186 this.setStatus('success')
187 }
188 } else if (this.connection) {
189 // Streaming adapter path
190 const mergedData = { ...this.body, ...input }
191 const stream = this.connection.connect(
192 [],
193 mergedData,
194 signal,
195 this.createRunContext(runId),
196 )
197 await this.processStream(stream, runId)
198 } else {
199 throw new Error(
200 'GenerationClient requires either a connection or fetcher option',
201 )
202 }
203 if (!signal.aborted && this.status === 'success') {
204 // Bump progress to 100 on successful completion so devtools
205 // snapshots reflect the final state. The bridge mirrors this in
206 // the run's recorded progress, but the snapshot reads `progress`
207 // from the client's core state.
208 this.progress = completeProgressValue(this.progress)
209 this.devtoolsBridge.finishRun(
210 this.devtoolsBridge.getActiveRunId() ?? runId,
211 'run:completed',
212 'completed',
213 )
214 }
215 } catch (err: unknown) {
216 if (signal.aborted) return
217 const error = err instanceof Error ? err : new Error(String(err))

Callers

nothing calls this directly

Calls 14

mountDevtoolsMethod · 0.95
setIsLoadingMethod · 0.95
setStatusMethod · 0.95
setErrorMethod · 0.95
processStreamMethod · 0.95
setResultMethod · 0.95
createRunContextMethod · 0.95
parseSSEResponseFunction · 0.90
completeProgressValueFunction · 0.85
connectMethod · 0.65
beginRunMethod · 0.45
ensureRunStartedMethod · 0.45

Tested by

no test coverage detected