feat: add readServerResource() and listServerResources() api to App (#470)
* feat: add readServerResource() and listServerResources() api to App class * test: add listServerResources and readServerResource integration tests
This commit is contained in:
parent
62c75203b6
commit
4f9a7ebc74
1 changed files with 502 additions and 40 deletions
|
|
@ -297,6 +297,60 @@ async function App_callServerTool_fetchWeather(app: App) {
|
|||
//#endregion App_callServerTool_fetchWeather
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Read a video resource and play it.
|
||||
*/
|
||||
async function App_readServerResource_playVideo(
|
||||
app: App,
|
||||
videoElement: HTMLVideoElement,
|
||||
) {
|
||||
//#region App_readServerResource_playVideo
|
||||
try {
|
||||
const result = await app.readServerResource({
|
||||
uri: "videos://bunny-1mb",
|
||||
});
|
||||
const content = result.contents[0];
|
||||
if (content && "blob" in content) {
|
||||
const binary = Uint8Array.from(atob(content.blob), (c) =>
|
||||
c.charCodeAt(0),
|
||||
);
|
||||
const url = URL.createObjectURL(
|
||||
new Blob([binary], { type: content.mimeType || "video/mp4" }),
|
||||
);
|
||||
videoElement.src = url;
|
||||
videoElement.play();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to read resource:", error);
|
||||
}
|
||||
//#endregion App_readServerResource_playVideo
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Discover available videos and build a picker UI.
|
||||
*/
|
||||
async function App_listServerResources_buildPicker(
|
||||
app: App,
|
||||
selectElement: HTMLSelectElement,
|
||||
) {
|
||||
//#region App_listServerResources_buildPicker
|
||||
try {
|
||||
const result = await app.listServerResources({});
|
||||
const videoResources = result.resources.filter((r) =>
|
||||
r.mimeType?.startsWith("video/"),
|
||||
);
|
||||
videoResources.forEach((resource) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = resource.uri;
|
||||
option.textContent = resource.description || resource.name;
|
||||
selectElement.appendChild(option);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to list resources:", error);
|
||||
}
|
||||
//#endregion App_listServerResources_buildPicker
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Send a text message from user interaction.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue