Gateway: multi-select answers and a summary endpoint for the complication
This commit is contained in:
1 parent
69858d4719
commit
830bf4f139
5 files changed
+98
-7
No files matched your search
+13
-2
@@ -18,7 +18,7 @@ import type { Hub } from "./hub.ts";
|
||||
import { HostError, type T3Host } from "./t3/host.ts";
|
||||
import { RpcError } from "./t3/rpc.ts";
|
||||
import type { ApprovalDecision, ModelSelection, ThreadShell } from "./t3/types.ts";
|
||||
import { buildFeed, buildThreadDetail, hostSummary, type SummaryCache } from "./views.ts";
|
||||
import { buildFeed, buildSummary, buildThreadDetail, hostSummary, type SummaryCache } from "./views.ts";
|
||||
|
||||
export interface ApiDeps {
|
||||
db: Db;
|
||||
@@ -135,6 +135,10 @@ export function createRoutes({ db, hub, cache, log = () => {} }: ApiDeps) {
|
||||
GET: authed(async () => json(await buildFeed(hub.list(), cache))),
|
||||
},
|
||||
|
||||
"/v1/summary": {
|
||||
GET: authed(() => json(buildSummary(hub.list()))),
|
||||
},
|
||||
|
||||
"/v1/projects": {
|
||||
GET: authed(() =>
|
||||
json({
|
||||
@@ -211,7 +215,7 @@ export function createRoutes({ db, hub, cache, log = () => {} }: ApiDeps) {
|
||||
if ("decision" in body) {
|
||||
if (!DECISIONS.has(body.decision)) throw new HttpError(400, "Unknown decision.");
|
||||
await host.dispatch({ ...base, decision: body.decision });
|
||||
} else if ("answers" in body && body.answers && typeof body.answers === "object") {
|
||||
} else if ("answers" in body && isAnswers(body.answers)) {
|
||||
await host.dispatch({ ...base, answers: body.answers });
|
||||
} else {
|
||||
throw new HttpError(400, "Send a decision or answers.");
|
||||
@@ -242,6 +246,13 @@ export function createRoutes({ db, hub, cache, log = () => {} }: ApiDeps) {
|
||||
};
|
||||
}
|
||||
|
||||
const isAnswers = (value: unknown): value is Record<string, string | string[]> =>
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
Object.values(value).every(
|
||||
(answer) => typeof answer === "string" || (Array.isArray(answer) && answer.every((item) => typeof item === "string")),
|
||||
);
|
||||
|
||||
async function readBody<T>(req: Request): Promise<Partial<T> & T> {
|
||||
try {
|
||||
return (await req.json()) as Partial<T> & T;
|
||||
|
||||
Reference in new issue
Block a user