Skip to content
Merged
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
6 changes: 5 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
const isProd = process.env.NODE_ENV === "production";

const services = [
// { name: "api", cmd: ["bun", "run", ...(isProd ? [] : ["--hot"]), "packages/api_platform/src/index.ts"], color: chalk.blue },
{
name: "api",
cmd: ["bun", "run", ...(isProd ? [] : ["--hot"]), "packages/api_platform/src/index.ts"],
color: chalk.blue,
},
{
name: "backend",
cmd: ["bun", "run", ...(isProd ? [] : ["--hot"]), "packages/backend/src/index.ts"],
Expand All @@ -27,7 +31,7 @@
let buf = "";
(async () => {
for (;;) {
const { done, value } = await reader.read();

Check warning on line 34 in index.ts

View workflow job for this annotation

GitHub Actions / Lint (Oxlint)

eslint(no-await-in-loop)

Unexpected `await` inside a loop.
if (done) break;
buf += decoder.decode(value, { stream: true });
const lines = buf.split("\n");
Expand Down
2 changes: 2 additions & 0 deletions packages/api_platform/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpMiddleware, HttpServer } from "@effect/platform";
import { BunContext, BunHttpServer, BunRuntime } from "@effect/platform-bun";
import { Effect, flow, Layer } from "effect";
import { fromEnv as LedgerServiceLive } from "ledger";
import { ResolverServiceLive } from "resolver";
import { VaultServiceLive } from "vault";

Expand Down Expand Up @@ -29,6 +30,7 @@ const AllServices = Layer.mergeAll(
AppConfigLive,
VaultServiceLive,
ResolverServiceLive,
LedgerServiceLive,
BunContext.layer,
);
const AllServicesAndHttpServer = Layer.mergeAll(AllServices, HttpServerLayer);
Expand Down
13 changes: 2 additions & 11 deletions packages/api_platform/src/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ProviderModelPair } from "resolver";
import type { VerifyApiKeyResult } from "common";

import { HttpMiddleware, HttpServerRequest } from "@effect/platform";
import { BadRequest, Unauthorized } from "@effect/platform/HttpApiError";
Expand All @@ -24,15 +24,6 @@ class ApiKeyVerificationError extends Data.TaggedError("ApiKeyVerificationError"
message?: string;
}> {}

interface VerifyResponse {
valid: boolean;
userId?: string;
providers?: string[];
fallbackProviderModelPair?: ProviderModelPair;
analysisTarget?: string;
error?: string;
}

const verifyApiKey = (backendUrl: string, apiKey: string) =>
Effect.tryPromise({
try: async () => {
Expand All @@ -44,7 +35,7 @@ const verifyApiKey = (backendUrl: string, apiKey: string) =>
if (!response.ok) {
throw new Error(`Backend returned ${response.status}`);
}
return (await response.json()) as VerifyResponse;
return (await response.json()) as VerifyApiKeyResult;
},
catch: (error) =>
new ApiKeyVerificationError({
Expand Down
23 changes: 5 additions & 18 deletions packages/api_platform/src/routes/v1/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,11 @@ export const responsesRouter = HttpRouter.empty.pipe(
Effect.gen(function* () {
const createResponseBody = yield* HttpServerRequest.schemaBodyJson(CreateResponseBodySchema);
yield* validateCreateResponseBody(createResponseBody);
const { userId, userProviders, fallbackProviderModelPair, analysisTarget } =
yield* RequestContext;
const params = yield* RequestContext;

if (createResponseBody.stream === true) {
const db = yield* DatabaseService;
const sseStream = yield* ResponsesService.createStream(
createResponseBody,
userId,
userProviders,
fallbackProviderModelPair,
analysisTarget,
);
const sseStream = yield* ResponsesService.createStream(createResponseBody, params);
return HttpServerResponse.stream(
sseStream.pipe(Stream.provideService(DatabaseService, db)),
{
Expand All @@ -128,19 +121,13 @@ export const responsesRouter = HttpRouter.empty.pipe(
);
}

const responsesObject = yield* ResponsesService.create(
createResponseBody,
userId,
userProviders,
fallbackProviderModelPair,
analysisTarget,
);
const responsesObject = yield* ResponsesService.create(createResponseBody, params);
return yield* HttpServerResponse.json(responsesObject);
}).pipe(
Effect.catchTags({
RequestValidationError: (err) =>
RequestValidationError: (err: RequestValidationError) =>
HttpServerResponse.json({ error: { message: err.message } }, { status: 400 }),
ResponseServiceError: (err) =>
ResponseServiceError: (err: ResponsesService.ResponseServiceError) =>
HttpServerResponse.json(
{ error: { message: err.message ?? "Internal Server Error" } },
{ status: 500, headers: { "x-enfinyte-error": `${err.name}: ${err}` } },
Expand Down
Loading
Loading