Skip to content

feat: update hono API #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Workflow can be used as middleware in any server framework, like `express`, `hon
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { createHonoHandler } from "@llama-flow/core/interrupter/hono";
import { filter } from "@llama-flow/core/stream/filter";
import { until } from "@llama-flow/core/stream/until";
import {
agentWorkflow,
startEvent,
Expand All @@ -168,9 +170,12 @@ const app = new Hono();
app.post(
"/workflow",
createHonoHandler(
agentWorkflow,
async (ctx) => startEvent(await ctx.req.text()),
stopEvent,
toolCallWorkflow,
async (ctx, sendEvent) => {
sendEvent(startEvent.with(await ctx.req.text()));
},
(stream) =>
filter(until(stream, stopEvent), (event) => stopEvent.include(event)),
),
);

Expand Down
12 changes: 9 additions & 3 deletions demo/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Hono } from "hono";
import { createWorkflow, workflowEvent } from "@llama-flow/core";
import { createHonoHandler } from "@llama-flow/core/hono";
import { html } from "hono/html";
import { toolCallWorkflow } from "../../workflows/tool-call-agent";
import { filter } from "@llama-flow/core/stream/filter";
import { until } from "@llama-flow/core/stream/until";

const app = new Hono();

Expand All @@ -16,9 +19,12 @@ workflow.handle([startEvent], ({ data }) => {
app.post(
"/workflow",
createHonoHandler(
workflow,
async (ctx) => startEvent.with(await ctx.req.text()),
stopEvent,
toolCallWorkflow,
async (ctx, sendEvent) => {
sendEvent(startEvent.with(await ctx.req.text()));
},
(stream) =>
filter(until(stream, stopEvent), (event) => stopEvent.include(event)),
),
);

Expand Down
2 changes: 1 addition & 1 deletion demo/cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
]
},
"exclude": ["node_modules", "dist", "tests"],
"include": ["src", "scripts"]
"include": ["src", "scripts", "../workflows"]
}
9 changes: 7 additions & 2 deletions demo/hono/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import {
startEvent,
stopEvent,
} from "../workflows/tool-call-agent.js";
import { until } from "@llama-flow/core/stream/until";
import { filter } from "@llama-flow/core/stream/filter";

const app = new Hono();

app.post(
"/workflow",
createHonoHandler(
toolCallWorkflow,
async (ctx) => startEvent.with(await ctx.req.text()),
stopEvent,
async (ctx, sendEvent) => {
sendEvent(startEvent.with(await ctx.req.text()));
},
(stream) =>
filter(until(stream, stopEvent), (event) => stopEvent.include(event)),
),
);

Expand Down
11 changes: 8 additions & 3 deletions docs/llamaflow/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,20 @@ We provide tons of [middleware](#) and [integrations](#) to make it easy to ship
import { Hono } from "hono";
import { createHonoHandler } from "@llama-flow/core/interrupter/hono";
import { openaiChatWorkflow, startEvent, stopEvent } from "@/lib/workflow";
import { filter } from '@llama-flow/core/stream/filter'
import { until } from '@llama-flow/core/stream/until'

const app = new Hono();

app.post(
"/workflow",
createHonoHandler(
openaiChatWorkflow,
async (ctx) => startEvent.with(ctx.req.json()),
stopEvent,
toolCallWorkflow,
async (ctx, sendEvent) => {
sendEvent(startEvent.with(await ctx.req.text()));
},
(stream) =>
filter(until(stream, stopEvent), (event) => stopEvent.include(event)),
),
);

Expand Down
30 changes: 13 additions & 17 deletions packages/core/src/hono.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import type { Context, Handler } from "hono";
import type {
Workflow,
WorkflowEvent,
WorkflowEventData,
import {
type Workflow,
type WorkflowEventData,
WorkflowStream,
} from "@llama-flow/core";
import { runWorkflow } from "./stream/run";

export const createHonoHandler = <Start, Stop>(
workflow: Workflow,
getStart: (
initEvent: (
c: Context,
) => WorkflowEventData<Start> | Promise<WorkflowEventData<Start>>,
stopEvent: WorkflowEvent<Stop>,
wrapStopEvent?: (c: Context, stop: Stop) => Response,
sendEvent: (...events: WorkflowEventData<any>[]) => void,
) => void | Promise<void>,
handleStream: (stream: WorkflowStream) => WorkflowStream,
): Handler => {
if (!wrapStopEvent) {
wrapStopEvent = (c, stop) => {
return c.json(stop as any);
};
}
return async (c) => {
const stop = await runWorkflow(workflow, await getStart(c), stopEvent);
return wrapStopEvent(c, stop.data);
return async (ctx) => {
const { stream, sendEvent } = workflow.createContext();
await initEvent(ctx, sendEvent);
const resultStream = handleStream(stream);
return resultStream.toResponse();
};
};
19 changes: 6 additions & 13 deletions packages/core/src/stream/filter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import type {
WorkflowEvent,
WorkflowEventData,
WorkflowStream,
} from "@llama-flow/core";
import type { WorkflowEventData, WorkflowStream } from "@llama-flow/core";

export function filter<
Event extends WorkflowEventData<any>,
Final extends Event,
>(
stream: ReadableStream<Event> | WorkflowStream<Event>,
cond: (event: Event) => event is Final,
): ReadableStream<Final> | WorkflowStream<Final> {
export function filter(
stream: WorkflowStream,
cond: (event: WorkflowEventData<any>) => boolean,
): WorkflowStream {
return stream.pipeThrough(
new TransformStream<Event, Final>({
new TransformStream({
transform(event, controller) {
if (cond(event)) {
controller.enqueue(event);
Expand Down
Loading