Content Upload And Resolution
Binary content has two separate middleware steps:
- upload user-provided bytes into a durable or provider-native reference,
- resolve HPD's internal reference into content the current provider can read.
This split lets thread history keep stable references while each model turn still receives provider-facing content.
Upload User Content
ContentLifecycleMiddleware runs before the message turn. It scans the user message for DataContent with bytes, including typed content such as images, audio, documents, and video.
using HPD.Agent;
using Microsoft.Extensions.AI;
var assistant = new AgentConfig()
.WithChatClient(chatClient);
await using var runtime = await new AgentRuntimeBuilder()
.AddAgent("assistant", assistant)
.BuildAsync();
var image = new DataContent(await File.ReadAllBytesAsync("diagram.png"), "image/png")
{
Name = "diagram.png"
};
await runtime.RunAsync(new UserMessagesInputEvent
{
Messages =
[
new ChatMessage(ChatRole.User,
[
new TextContent("Describe this diagram."),
image
])
],
SessionId = "session-1",
ThreadId = "main"
});The upload path depends on the run and provider configuration:
| Upload path | Resulting content |
|---|---|
| Hosted file client | HostedFileContent |
| Framework content store | StoredContentReferenceContent over a ContentAddress |
| No configured path or failed required path | Original DataContent remains |
AgentRunConfig.UploadStrategy controls which path is required or preferred:
| Strategy | Behavior |
|---|---|
Auto | Prefer hosted files when available; fall back to the content store when possible. |
Hosted | Require a hosted file client. |
Local | Require an IContentStore. |
Hosted upload can come from a provider-created hosted-file client, AgentRunConfig.OverrideHostedFileClient, or the agent's resolved client set. Local upload uses the runtime's content store, which is in memory by default. Use AgentRuntimeBuilder.WithContentStore(...) to select a different store; every agent the runtime runs shares it.
Resolve Internal References
The same ContentLifecycleMiddleware runs before each model iteration and looks for the internal runtime transport part StoredContentReferenceContent:
ContentAddress = ContentScope + ContentIdProviders should not receive that runtime transport part directly. The middleware turns it into the best provider-facing shape available for the current run:
| Resolution path | Provider sees |
|---|---|
| Temporary read URI from the content store | UriContent(directUri, mediaType) |
| Hosted file upload from the content stream | HostedFileContent |
| Buffered fallback | DataContent |
If resolution fails, the stored reference is replaced in the model projection with a TextContent unavailable marker and ContentReferenceResolutionFailedEvent is emitted. The canonical stored reference remains in thread history.
Why There Are Two Steps
Upload and resolution answer different questions:
| Step | Question |
|---|---|
| Upload | Where should these user bytes live after the initial message? |
| Resolution | What content shape can this provider consume right now? |
This is especially useful for sessions and threads. A thread persists the typed stored-content reference, then later resolves it as a direct URL, hosted file, or buffered bytes depending on the provider and runtime environment. Observation events use a synthetic stored-content://{contentId} address that is never sent to a provider.
Thread Scope
Local content-store upload and reference resolution are scoped to the active thread:
sessionId + threadIdNormal RunAsync(..., sessionId: ...) execution supplies that context through the active thread. When you construct UserMessagesInputEvent directly, include both SessionId and ThreadId so upload and resolution use the same durable scope.
Sibling threads do not automatically resolve each other's local content references. Forking and replay should preserve the thread path that owns the content reference.
Manage A Thread's Files
runtime.Content stores, lists, downloads, and deletes files in a thread's scope of the runtime's content store, outside a run. Files uploaded this way are user content, the same as bytes uploaded from a message.
var thread = new ThreadKey(sessionId, "main");
await using var file = File.OpenRead("notes.txt");
var info = await runtime.Content.UploadAsync(thread, file, "notes.txt", "text/plain");
foreach (var item in await runtime.Content.ListAsync(thread))
Console.WriteLine($"{item.Name} {item.ContentType} {item.SizeBytes} bytes");
var download = await runtime.Content.DownloadAsync(thread, info.Address.ContentId);
if (download is not null)
await File.WriteAllBytesAsync("copy.txt", download.Data);
await runtime.Content.DeleteAsync(thread, info.Address.ContentId);| Method | Returns |
|---|---|
UploadAsync(thread, stream, fileName, contentType) | The stored file's ContentInfo. contentType defaults to application/octet-stream. |
ListAsync(thread) | The thread's files. |
DownloadAsync(thread, contentId) | An AgentContentFile with the file's Info and Data, or null when it does not exist. |
DeleteAsync(thread, contentId) | Whether a file was deleted. |
Deleting a thread removes its files, and deleting a session removes the files of all its threads. The hosted content routes call the same API.
Events
Upload and resolution emit observability events:
| Event family | Meaning |
|---|---|
ContentUploadedEvent | User bytes were stored in IContentStore. |
HostedFileUploadedEvent | User bytes were uploaded to a provider hosted-file client. |
ContentUploadFailedEvent | Local upload failed or a required local path was unavailable. |
HostedFileUploadFailedEvent | Hosted upload failed or a required hosted path was unavailable. |
ContentReferenceResolvedEvent | An internal HPD reference was resolved for provider use. |
ContentReferenceResolutionFailedEvent | An internal HPD reference could not be resolved. |
Use these events for UI state, diagnostics, and tests. Do not infer upload or resolution success only from the final content type.
Audio Interaction
Audio input participates in the same content flow, but audio runtime detection happens before content upload. That lets the audio runtime preserve media identity, content index, name, media type, size, and transcript metadata even if later middleware turns the original AudioContent into a UriContent or hosted file reference.
For the audio-specific model behavior, see Speech To Text Input and Audio Runtime Attachment.