初始化提交
Some checks failed
CI / Check / macos-latest (push) Has been cancelled
CI / Check / ubuntu-latest (push) Has been cancelled
CI / Check / windows-latest (push) Has been cancelled
CI / Test / macos-latest (push) Has been cancelled
CI / Test / ubuntu-latest (push) Has been cancelled
CI / Test / windows-latest (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Security Audit (push) Has been cancelled
CI / Secrets Scan (push) Has been cancelled
CI / Install Script Smoke Test (push) Has been cancelled
Some checks failed
CI / Check / macos-latest (push) Has been cancelled
CI / Check / ubuntu-latest (push) Has been cancelled
CI / Check / windows-latest (push) Has been cancelled
CI / Test / macos-latest (push) Has been cancelled
CI / Test / ubuntu-latest (push) Has been cancelled
CI / Test / windows-latest (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Security Audit (push) Has been cancelled
CI / Secrets Scan (push) Has been cancelled
CI / Install Script Smoke Test (push) Has been cancelled
This commit is contained in:
33
sdk/javascript/examples/streaming.js
Normal file
33
sdk/javascript/examples/streaming.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 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);
|
||||
Reference in New Issue
Block a user