Project created from basic template
This commit is contained in:
11
src/server/index.ts
Normal file
11
src/server/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Hono } from "hono";
|
||||
|
||||
import { rpcApp } from "./routes/rpc";
|
||||
import { clientEntry } from "./routes/client-entry";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
app.route("/rpc", rpcApp);
|
||||
app.get("/*", clientEntry);
|
||||
|
||||
export default app;
|
41
src/server/lib/create-kv.ts
Normal file
41
src/server/lib/create-kv.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { createStorage, StorageValue, WatchEvent } from "unstorage";
|
||||
import fsDriver from "unstorage/drivers/fs";
|
||||
|
||||
const STORAGE_PATH = "./.storage"; // It is .gitignored
|
||||
|
||||
export function createKV<T extends StorageValue>(name: string) {
|
||||
const storage = createStorage<T>({
|
||||
driver: fsDriver({ base: `${STORAGE_PATH}/${name}` }),
|
||||
});
|
||||
|
||||
// Async generator to play work well with oRPC live queries
|
||||
async function* subscribe() {
|
||||
let resolve: (value: { event: WatchEvent; key: string }) => void;
|
||||
let promise = new Promise<{ event: WatchEvent; key: string }>(
|
||||
(r) => (resolve = r)
|
||||
);
|
||||
|
||||
const unwatch = await storage.watch((event, key) => {
|
||||
resolve({ event, key });
|
||||
promise = new Promise<{ event: WatchEvent; key: string }>(
|
||||
(r) => (resolve = r)
|
||||
);
|
||||
});
|
||||
|
||||
try {
|
||||
while (true) yield await promise;
|
||||
} finally {
|
||||
await unwatch();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...storage,
|
||||
getAllItems: async () => {
|
||||
const keys = await storage.getKeys();
|
||||
const values = await storage.getItems(keys);
|
||||
return values.map(({ value }) => value);
|
||||
},
|
||||
subscribe,
|
||||
};
|
||||
}
|
28
src/server/lib/openai.ts
Normal file
28
src/server/lib/openai.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { jsonrepair } from "jsonrepair";
|
||||
import { z } from "zod";
|
||||
import { makeParseableResponseFormat } from "openai/lib/parser";
|
||||
|
||||
import type { AutoParseableResponseFormat } from "openai/lib/parser";
|
||||
import type { ResponseFormatJSONSchema } from "openai/resources";
|
||||
|
||||
export function zodResponseFormat<ZodInput extends z.ZodType>(
|
||||
zodObject: ZodInput,
|
||||
name: string,
|
||||
props?: Omit<
|
||||
ResponseFormatJSONSchema.JSONSchema,
|
||||
"schema" | "strict" | "name"
|
||||
>
|
||||
): AutoParseableResponseFormat<z.infer<ZodInput>> {
|
||||
return makeParseableResponseFormat(
|
||||
{
|
||||
type: "json_schema",
|
||||
json_schema: {
|
||||
...props,
|
||||
name,
|
||||
strict: true,
|
||||
schema: z.toJSONSchema(zodObject, { target: "draft-7" }),
|
||||
},
|
||||
},
|
||||
(content) => zodObject.parse(JSON.parse(jsonrepair(content)))
|
||||
);
|
||||
}
|
33
src/server/routes/client-entry.tsx
Normal file
33
src/server/routes/client-entry.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
/** @jsxImportSource hono/jsx */
|
||||
import type { Context } from "hono";
|
||||
import viteReact from "@vitejs/plugin-react";
|
||||
|
||||
import type { BlankEnv } from "hono/types";
|
||||
|
||||
export function clientEntry(c: Context<BlankEnv>) {
|
||||
return c.html(
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charSet="utf-8" />
|
||||
<meta content="width=device-width, initial-scale=1" name="viewport" />
|
||||
<title>New Quest</title>
|
||||
{import.meta.env.PROD ? (
|
||||
<script src="/static/main.js" type="module" />
|
||||
) : (
|
||||
<>
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: viteReact.preambleCode.replace("__BASE__", "/"),
|
||||
}}
|
||||
type="module"
|
||||
/>
|
||||
<script src="/src/client/main.tsx" type="module" />
|
||||
</>
|
||||
)}
|
||||
</head>
|
||||
<body>
|
||||
<div id="root" />
|
||||
</body>
|
||||
</html>,
|
||||
);
|
||||
}
|
21
src/server/routes/rpc.ts
Normal file
21
src/server/routes/rpc.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { RPCHandler } from "@orpc/server/fetch";
|
||||
|
||||
import { router } from "@/server/rpc";
|
||||
import { Hono } from "hono";
|
||||
|
||||
export const rpcApp = new Hono();
|
||||
|
||||
const handler = new RPCHandler(router);
|
||||
|
||||
rpcApp.use("/*", async (c, next) => {
|
||||
const { matched, response } = await handler.handle(c.req.raw, {
|
||||
prefix: "/rpc",
|
||||
});
|
||||
|
||||
if (matched) {
|
||||
return c.newResponse(response.body, response);
|
||||
}
|
||||
|
||||
await next();
|
||||
return;
|
||||
});
|
95
src/server/rpc/demo/ai.ts
Normal file
95
src/server/rpc/demo/ai.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import OpenAI from "openai";
|
||||
import { os } from "@orpc/server";
|
||||
import { z } from "zod";
|
||||
|
||||
import { zodResponseFormat } from "@/server/lib/openai";
|
||||
|
||||
if (!process.env.OPENAI_BASE_URL) {
|
||||
throw new Error("OPENAI_BASE_URL is not set");
|
||||
}
|
||||
|
||||
if (!process.env.OPENAI_API_KEY) {
|
||||
throw new Error("OPENAI_API_KEY is not set");
|
||||
}
|
||||
|
||||
const openai = new OpenAI({
|
||||
baseURL: process.env.OPENAI_BASE_URL,
|
||||
apiKey: process.env.OPENAI_API_KEY,
|
||||
});
|
||||
|
||||
if (!process.env.OPENAI_DEFAULT_MODEL) {
|
||||
throw new Error("OPENAI_DEFAULT_MODEL is not set");
|
||||
}
|
||||
|
||||
const DEFAULT_MODEL = process.env.OPENAI_DEFAULT_MODEL;
|
||||
|
||||
const ChatCompletionInputSchema = z.object({
|
||||
message: z.string(),
|
||||
systemPrompt: z.string().optional(),
|
||||
});
|
||||
|
||||
const GeneratePersonInputSchema = z.object({
|
||||
prompt: z.string(),
|
||||
});
|
||||
|
||||
const complete = os
|
||||
.input(ChatCompletionInputSchema)
|
||||
.handler(async ({ input }) => {
|
||||
const { message, systemPrompt } = input;
|
||||
|
||||
const completion = await openai.chat.completions.create({
|
||||
model: DEFAULT_MODEL,
|
||||
messages: [
|
||||
...(systemPrompt
|
||||
? [{ role: "system" as const, content: systemPrompt }]
|
||||
: []),
|
||||
{ role: "user" as const, content: message },
|
||||
],
|
||||
});
|
||||
|
||||
return {
|
||||
response: completion.choices[0]?.message?.content || "",
|
||||
};
|
||||
});
|
||||
|
||||
// Object generation schemas only support nullability, not optionality.
|
||||
// Use .nullable() instead of .optional() for fields that may not have values.
|
||||
const DemoSchema = z.object({
|
||||
name: z.string().describe("The name of the person"),
|
||||
age: z.number().describe("The age of the person"),
|
||||
occupation: z.string().describe("The occupation of the person"),
|
||||
bio: z.string().describe("The bio of the person"),
|
||||
nickname: z
|
||||
.string()
|
||||
.nullable()
|
||||
.describe("The person's nickname, if they have one"),
|
||||
});
|
||||
|
||||
const generate = os
|
||||
.input(GeneratePersonInputSchema)
|
||||
.handler(async ({ input }) => {
|
||||
const completion = await openai.chat.completions.parse({
|
||||
model: DEFAULT_MODEL,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: `Generate a person based on this prompt: ${input.prompt}`,
|
||||
},
|
||||
],
|
||||
response_format: zodResponseFormat(DemoSchema, "person"),
|
||||
});
|
||||
|
||||
const person = completion.choices[0]?.message?.parsed;
|
||||
if (!person) {
|
||||
throw new Error("No parsed data received from OpenAI");
|
||||
}
|
||||
|
||||
return {
|
||||
person,
|
||||
};
|
||||
});
|
||||
|
||||
export const router = {
|
||||
complete,
|
||||
generate,
|
||||
};
|
7
src/server/rpc/demo/index.ts
Normal file
7
src/server/rpc/demo/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { router as storageRouter } from "./storage";
|
||||
import { router as aiRouter } from "./ai";
|
||||
|
||||
export const demo = {
|
||||
storage: storageRouter,
|
||||
ai: aiRouter,
|
||||
};
|
51
src/server/rpc/demo/storage.ts
Normal file
51
src/server/rpc/demo/storage.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { call, os } from "@orpc/server";
|
||||
import { z } from "zod";
|
||||
import { randomUUID } from "crypto";
|
||||
import { createKV } from "@/server/lib/create-kv";
|
||||
|
||||
const DemoSchema = z.object({
|
||||
id: z.string(),
|
||||
value: z.string(),
|
||||
});
|
||||
|
||||
type Demo = z.output<typeof DemoSchema>;
|
||||
|
||||
// createKV provides simple key-value storage with publisher/subscriber support
|
||||
// perfect for live queries and small amounts of data
|
||||
const kv = createKV<Demo>("demo");
|
||||
|
||||
// Handler with input validation using .input() and schema
|
||||
const create = os
|
||||
.input(DemoSchema.omit({ id: true }))
|
||||
.handler(async ({ input }) => {
|
||||
const id = randomUUID();
|
||||
const item = { id, value: input.value };
|
||||
await kv.setItem(id, item);
|
||||
});
|
||||
|
||||
const remove = os.input(z.string()).handler(async ({ input }) => {
|
||||
await kv.removeItem(input);
|
||||
});
|
||||
|
||||
// Handler without input - returns all items
|
||||
const list = os.handler(async () => {
|
||||
return kv.getAllItems();
|
||||
});
|
||||
|
||||
// Live data stream using generator function
|
||||
// Yields initial data, then subscribes to changes for real-time updates
|
||||
const live = {
|
||||
list: os.handler(async function* ({ signal }) {
|
||||
yield call(list, {}, { signal });
|
||||
for await (const _ of kv.subscribe()) {
|
||||
yield call(list, {}, { signal });
|
||||
}
|
||||
}),
|
||||
};
|
||||
|
||||
export const router = {
|
||||
create,
|
||||
remove,
|
||||
list,
|
||||
live,
|
||||
};
|
5
src/server/rpc/index.ts
Normal file
5
src/server/rpc/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { demo } from "./demo";
|
||||
|
||||
export const router = {
|
||||
demo,
|
||||
};
|
Reference in New Issue
Block a user