Javascript SDK
Reference
asStream

asStream

Provides access to the callbacks that send data back to Llama Workspace. The method accepts an async function which provides pushText as a parameter. When pushText is invoked, a chunk of text is sent back to Llama Workspace and displayed to the user.

Parameters

  • processor: ({ pushText }: ProcessorCallbacks) => void - The processor function that you will use to send data back to the client.

Returns

  • Promise<ReadableStream<Uint8Array>>

Example

import { LlamaWorkspace } from "llamaworkspace";
 
// app is an instance of express, koa, hono or any other web framework
app.post("/", async (req) => {
  // ...instantiation code...
  const stream = llamaWorkspace.asStream(async ({ pushText }) => {
    // Your internal logic that eventually generates streaming data.
 
    const myInternalStream = await myAIAgentProcess();
 
    for await (const chunk of myInternalStream) {
      // Every chunk of text generated by your code is sent back to Llama Workspace
      pushText(chunk);
    }
  });
 
  return new Response(stream, {
    headers: {
      "Content-Type": "text/event-stream",
    },
  });
});