-
Notifications
You must be signed in to change notification settings - Fork 83
[Automated][tcgc] Document versionsEnum and SSE metadata in emitter guideline #5072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
347027e
docs(tcgc): document versionsEnum and SSE metadata in emitter guideline
github-actions[bot] b08bae8
docs(tcgc): add streaming/SSE authoring doc under howtos/Generate cli…
Copilot a98a782
docs(tcgc): replace language code snippets with // unsupported in str…
Copilot 612eb08
docs(tcgc): add ClientTabs with language sections to all SSE examples
Copilot 2aea88a
docs(tcgc): add ClientTabs with language sections to custom streaming…
Copilot 45a3c8b
Merge branch 'main' into doc-updater/tcgc-20260727-a3d19c6e3f219a75
tadelesh 8e1197d
format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
348 changes: 348 additions & 0 deletions
348
website/src/content/docs/docs/howtos/Generate client libraries/13streaming.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,348 @@ | ||
| --- | ||
| title: Streaming Operations | ||
| llmstxt: true | ||
| --- | ||
|
|
||
| import { ClientTabs, ClientTabItem } from "@components/client-tabs"; | ||
|
lirenhe marked this conversation as resolved.
|
||
|
|
||
| This doc explains how to author TypeSpec streaming operations and what client emitters will generate for them. | ||
|
|
||
| TypeSpec supports three kinds of streaming bodies via the `@typespec/streams`, `@typespec/sse`, and `@typespec/events` packages: | ||
|
|
||
| - **`JsonlStream<T>`** — a newline-delimited JSON stream where every line is a JSON-encoded `T`. | ||
| - **`SSEStream<TEvents>`** — a server-sent event stream (`text/event-stream`) carrying heterogeneous events described by an `@events` union. | ||
| - **`HttpStream<T, ContentType>`** — a generic streaming body; using `ContentType = "text/event-stream"` with an `@events` union activates the same SSE metadata as `SSEStream`. | ||
|
|
||
| :::note | ||
| Import the required packages in your TypeSpec spec: | ||
|
|
||
| ```tsp | ||
| import "@typespec/streams"; | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
| ``` | ||
| ::: | ||
|
|
||
| ## JSONL streaming response | ||
|
|
||
| Use `JsonlStream<T>` when your service responds with newline-delimited JSON lines, each encoding a value of type `T`. | ||
|
|
||
| <ClientTabs> | ||
|
|
||
| ```typespec | ||
| import "@typespec/streams"; | ||
|
|
||
| using TypeSpec.Streams; | ||
|
|
||
| model Chunk { | ||
| text: string; | ||
| } | ||
|
|
||
| @post | ||
| op generate(@body request: { prompt: string }): JsonlStream<Chunk>; | ||
| ``` | ||
|
|
||
| ```python | ||
| # unsupported | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```typescript | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```java | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```go | ||
| // unsupported | ||
| ``` | ||
|
|
||
| </ClientTabs> | ||
|
|
||
| ## SSE streaming response | ||
|
|
||
| Use `SSEStream<TEvents>` to define a server-sent event stream. Events are described by a union decorated with `@Events.events`. Each named variant becomes an SSE event type; unnamed variants produce `message` events. | ||
|
|
||
| <ClientTabs> | ||
|
|
||
| ```typespec | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
|
|
||
| using TypeSpec.SSE; | ||
| using TypeSpec.Events; | ||
|
|
||
| model ResponseCreated { | ||
| id: string; | ||
| } | ||
|
|
||
| model ResponseDelta { | ||
| delta: string; | ||
| } | ||
|
|
||
| @events | ||
| union ResponseEvents { | ||
| @contentType("application/json") | ||
| responseCreated: ResponseCreated, | ||
|
|
||
| @contentType("application/json") | ||
| responseDelta: ResponseDelta, | ||
|
|
||
| @contentType("text/plain") | ||
| @terminalEvent | ||
| "[DONE]", | ||
| } | ||
|
|
||
| @post | ||
| op stream(@body request: { prompt: string }): SSEStream<ResponseEvents>; | ||
| ``` | ||
|
|
||
| ```python | ||
| # unsupported | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```typescript | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```java | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```go | ||
| // unsupported | ||
| ``` | ||
|
|
||
| </ClientTabs> | ||
|
|
||
| ### Terminal events | ||
|
|
||
| Mark a variant with `@terminalEvent` to indicate that receiving this event signals the end of the stream. Client emitters use this to know when to stop reading and close the connection. | ||
|
|
||
| <ClientTabs> | ||
|
|
||
| ```typespec | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
|
|
||
| using TypeSpec.SSE; | ||
| using TypeSpec.Events; | ||
|
|
||
| model DeltaEvent { | ||
| delta: string; | ||
| } | ||
|
|
||
| @events | ||
| union ChatEvents { | ||
| @contentType("application/json") | ||
| delta: DeltaEvent, | ||
|
|
||
| @contentType("text/plain") | ||
| @terminalEvent | ||
| "[DONE]", | ||
| } | ||
|
|
||
| @post | ||
| op chat(@body request: { message: string }): SSEStream<ChatEvents>; | ||
| ``` | ||
|
|
||
| ```python | ||
| # unsupported | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```typescript | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```java | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```go | ||
| // unsupported | ||
| ``` | ||
|
|
||
| </ClientTabs> | ||
|
|
||
| ### Event envelopes | ||
|
|
||
| When your SSE event wraps a separate payload, use `@Events.data` on the payload property to distinguish the envelope from its content. Emitters expose both the envelope type and the unwrapped payload type through `sseMetadata`. | ||
|
|
||
| <ClientTabs> | ||
|
|
||
| ```typespec | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
|
|
||
| using TypeSpec.SSE; | ||
| using TypeSpec.Events; | ||
|
|
||
| model ChatMessage { | ||
| role: string; | ||
| content: string; | ||
| } | ||
|
|
||
| @events | ||
| union ChatEvents { | ||
| @contentType("application/json") | ||
| message: { done: false, @data message: ChatMessage }, | ||
|
|
||
| @contentType("text/plain") | ||
| @terminalEvent | ||
| done: { done: true }, | ||
| } | ||
|
|
||
| @post | ||
| op chat(@body request: { message: string }): SSEStream<ChatEvents>; | ||
| ``` | ||
|
|
||
| ```python | ||
| # unsupported | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```typescript | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```java | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```go | ||
| // unsupported | ||
| ``` | ||
|
|
||
| </ClientTabs> | ||
|
|
||
| ### Unnamed (message) events | ||
|
|
||
| A variant without a name in the union produces an SSE `message` event — the `event:` field is omitted on the wire. Use this when your service emits a single homogeneous event type. | ||
|
|
||
| <ClientTabs> | ||
|
|
||
| ```typespec | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
|
|
||
| using TypeSpec.SSE; | ||
| using TypeSpec.Events; | ||
|
|
||
| model Info { | ||
| desc: string; | ||
| } | ||
|
|
||
| @events | ||
| union BasicEvents { | ||
| @contentType("application/json") | ||
| Info, | ||
| } | ||
|
|
||
| op receive(): SSEStream<BasicEvents>; | ||
| ``` | ||
|
|
||
| ```python | ||
| # unsupported | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```typescript | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```java | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```go | ||
| // unsupported | ||
| ``` | ||
|
|
||
| </ClientTabs> | ||
|
|
||
| ## SSE streaming request | ||
|
|
||
| `SSEStream` can also be used as a request body when the client sends an event stream to the service. | ||
|
|
||
| <ClientTabs> | ||
|
|
||
| ```typespec | ||
| import "@typespec/sse"; | ||
| import "@typespec/events"; | ||
|
|
||
| using TypeSpec.SSE; | ||
| using TypeSpec.Events; | ||
|
|
||
| model InputEvent { | ||
| data: string; | ||
| } | ||
|
|
||
| @events | ||
| union InputEvents { | ||
| @contentType("application/json") | ||
| inputEvent: InputEvent, | ||
| } | ||
|
|
||
| op send(stream: SSEStream<InputEvents>): void; | ||
| ``` | ||
|
|
||
| ```python | ||
| # unsupported | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```typescript | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```java | ||
| // unsupported | ||
| ``` | ||
|
|
||
| ```go | ||
| // unsupported | ||
| ``` | ||
|
|
||
| </ClientTabs> | ||
|
|
||
| ## Custom streaming body | ||
|
|
||
| For content types other than JSONL or `text/event-stream`, define a model with `@streamOf` and a `@body bytes` property. | ||
|
|
||
| ```typespec | ||
| import "@typespec/streams"; | ||
|
|
||
| using TypeSpec.Streams; | ||
|
|
||
| model AudioChunk { id: string; } | ||
|
|
||
| @streamOf(AudioChunk) | ||
| model AudioStream { | ||
| @header contentType: "audio/mpeg"; | ||
| @body body: bytes; | ||
| } | ||
|
|
||
| op synthesize(@body request: { text: string }): AudioStream; | ||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.