Files
rightnow-ai--openfang/sdk/javascript/examples/streaming.js
T
wehub-resource-sync d93385b373
CI / Check / macos-latest (push) Waiting to run
CI / Check / ubuntu-latest (push) Waiting to run
CI / Check / windows-latest (push) Waiting to run
CI / Test / macos-latest (push) Waiting to run
CI / Test / ubuntu-latest (push) Waiting to run
CI / Test / windows-latest (push) Waiting to run
CI / Clippy (push) Waiting to run
CI / Format (push) Waiting to run
CI / Security Audit (push) Waiting to run
CI / Secrets Scan (push) Waiting to run
CI / Install Script Smoke Test (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 12:43:09 +08:00

34 lines
902 B
JavaScript

/**
* Streaming example — stream agent responses token by token.
*
* Usage:
* node streaming.js
*/
const { OpenFang } = require("../index");
async function main() {
const client = new OpenFang("http://localhost:3000");
// Create an agent
const agent = await client.agents.create({ template: "assistant" });
console.log("Agent:", agent.id);
// Stream the response
console.log("\n--- Streaming response ---");
for await (const event of client.agents.stream(agent.id, "Tell me a short story about a robot.")) {
if (event.type === "text_delta" && event.delta) {
process.stdout.write(event.delta);
} else if (event.type === "tool_call") {
console.log("\n[Tool call:", event.tool, "]");
} else if (event.type === "done") {
console.log("\n--- Done ---");
}
}
// Clean up
await client.agents.delete(agent.id);
}
main().catch(console.error);