Skip to content

add effect to the mix #2

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
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
29 changes: 19 additions & 10 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { MockLanguageModelV1 } from "ai/test";
import { streamText, simulateReadableStream, createDataStream } from "ai";
import { Effect, Runtime } from "effect";
import { runtime as appManagedRuntime } from "@/app/runtime";
import { UTApi } from "uploadthing/server";

const utapi = new UTApi({
token:
"eyJhcHBJZCI6ImFwcC0xIiwiYXBpS2V5Ijoic2tfZm9vIiwicmVnaW9ucyI6WyJmcmExIl19",
});

const generateChatStream = async () => {
console.log("Generating chat stream");
const generateChatStream = () =>
Effect.gen(function* () {
yield* Effect.log("Generating chat stream");

const dataStream = createDataStream({
execute: (dataStream) => {
Expand Down Expand Up @@ -48,7 +51,7 @@ const generateChatStream = async () => {
});

return dataStream;
};
});

/**
* This breaks the /api/chat route out from being "bundled" in fluid compute
Expand All @@ -58,14 +61,20 @@ const generateChatStream = async () => {
export const maxDuration = 799;

async function internalRouteHandler(req: Request) {
const stream = await generateChatStream();
const runtime = await appManagedRuntime.runtime();

return new Response(stream, {
headers: {
"Content-Type": "text/plain; charset=utf-8",
"X-Vercel-AI-Data-Stream": "v1",
},
});
return generateChatStream().pipe(
Effect.map(
(stream) =>
new Response(stream, {
headers: {
"Content-Type": "text/plain; charset=utf-8",
"X-Vercel-AI-Data-Stream": "v1",
},
})
),
Runtime.runPromise(runtime)
);
}

export async function POST(req: Request) {
Expand Down
23 changes: 22 additions & 1 deletion app/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import { Context, Data, Effect, Layer } from "effect";

const data = new Map<string, unknown>();

export const imaginaryAsyncDbClient = {
get: async <T>(key: string) => data.get(key) as T | undefined,
set: async <T>(key: string, value: T) => data.set(key, value),
};
};

export class ImaginaryDbClient extends Context.Tag("ImaginaryDbClient")<
ImaginaryDbClient,
typeof imaginaryAsyncDbClient
>() {
static Live = Layer.succeed(this, imaginaryAsyncDbClient);
}

export class ImaginaryDbError extends Data.TaggedError("ImaginaryDbError")<{
cause: unknown;
}> {}

export const runQuery = <T>(key: string) =>
Effect.flatMap(ImaginaryDbClient, (client) =>
Effect.tryPromise({
try: () => client.get<T>(key),
catch: (error) => new ImaginaryDbError({ cause: error }),
})
);
11 changes: 9 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Effect } from "effect";
import { runQuery } from "./db";
import { runtime } from "./runtime";
import { ChatTest } from "./chat";
import { imaginaryAsyncDbClient } from "./db";

export const dynamic = "force-dynamic";

Expand All @@ -23,7 +25,12 @@ export default async function Home() {
return null;
});

const dbData = await imaginaryAsyncDbClient.get("image");
const dbData = await runQuery("image").pipe(
Effect.tap((data) =>
Effect.log("Got data").pipe(Effect.annotateLogs({ data }))
),
runtime.runPromise
);

return (
<div>
Expand Down
4 changes: 4 additions & 0 deletions app/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ManagedRuntime } from "effect";
import { ImaginaryDbClient } from "./db";

export const runtime = ManagedRuntime.make(ImaginaryDbClient.Live)
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"preview": "next build && next start"
},
"dependencies": {
"@effect/language-service": "0.21.6",
"@effect/platform": "0.85.2",
"ai": "^4.3.16",
"effect": "3.16.8",
"next": "15.1.7",
"react": "19.0.0",
"react-dom": "19.0.0",
Expand All @@ -22,5 +25,11 @@
"@types/react-dom": "^19",
"typescript": "^5"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]"
}
}
}
32 changes: 32 additions & 0 deletions patches/effect.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
diff --git a/dist/cjs/internal/fiberRuntime.js b/dist/cjs/internal/fiberRuntime.js
index dee5633b073814a82a36070b2f84bf51c1a0806e..60462fcd9040a134ec97fbc74bd559114ec89e32 100644
--- a/dist/cjs/internal/fiberRuntime.js
+++ b/dist/cjs/internal/fiberRuntime.js
@@ -1137,7 +1137,7 @@ class FiberRuntime extends Effectable.Class {
// @ts-expect-error
cur = this.currentTracer.context(() => {
if (_version !== cur[core.EffectTypeId]._V) {
- return core.dieMessage(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
+ this.log(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`, internalCause.empty, LogLevel.Warning);
}
// @ts-expect-error
return this[cur._op](cur);
diff --git a/dist/esm/internal/fiberRuntime.js b/dist/esm/internal/fiberRuntime.js
index 13ca77a07dda3aca50eaa2507a7fa917a132535f..65c19754c80749e2b58f4040c5f229039c70388e 100644
--- a/dist/esm/internal/fiberRuntime.js
+++ b/dist/esm/internal/fiberRuntime.js
@@ -1,4 +1,4 @@
-import * as RA from "../Array.js";
+ import * as RA from "../Array.js";
import * as Boolean from "../Boolean.js";
import * as Chunk from "../Chunk.js";
import * as Context from "../Context.js";
@@ -1128,7 +1128,7 @@ export class FiberRuntime extends Effectable.Class {
// @ts-expect-error
cur = this.currentTracer.context(() => {
if (_version !== cur[core.EffectTypeId]._V) {
- return core.dieMessage(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
+ this.log(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`, internalCause.empty, LogLevel.Warning);
}
// @ts-expect-error
return this[cur._op](cur);
26 changes: 26 additions & 0 deletions patches/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/dist/cjs/internal/fiberRuntime.js b/dist/cjs/internal/fiberRuntime.js
index dee5633b073814a82a36070b2f84bf51c1a0806e..bc1001b09b125b9e7c6d441adb5da0c3e7c84015 100644
--- a/dist/cjs/internal/fiberRuntime.js
+++ b/dist/cjs/internal/fiberRuntime.js
@@ -1137,7 +1137,7 @@ class FiberRuntime extends Effectable.Class {
// @ts-expect-error
cur = this.currentTracer.context(() => {
if (_version !== cur[core.EffectTypeId]._V) {
- return core.dieMessage(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
+ console.warn(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`)
}
// @ts-expect-error
return this[cur._op](cur);
diff --git a/dist/esm/internal/fiberRuntime.js b/dist/esm/internal/fiberRuntime.js
index 13ca77a07dda3aca50eaa2507a7fa917a132535f..27d0de727cd9a5bc7c7a2d03d8eafa5e59cddc83 100644
--- a/dist/esm/internal/fiberRuntime.js
+++ b/dist/esm/internal/fiberRuntime.js
@@ -1128,7 +1128,7 @@ export class FiberRuntime extends Effectable.Class {
// @ts-expect-error
cur = this.currentTracer.context(() => {
if (_version !== cur[core.EffectTypeId]._V) {
- return core.dieMessage(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
+ console.warn(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`)
}
// @ts-expect-error
return this[cur._op](cur);
26 changes: 26 additions & 0 deletions patches/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/dist/cjs/internal/fiberRuntime.js b/dist/cjs/internal/fiberRuntime.js
index c126492b7f4feaf857d7b799deb997e2276511be..96438dd3dfe566aabc94853c0dcf0175e6943e52 100644
--- a/dist/cjs/internal/fiberRuntime.js
+++ b/dist/cjs/internal/fiberRuntime.js
@@ -1136,7 +1136,7 @@ class FiberRuntime extends Effectable.Class {
// @ts-expect-error
cur = this.currentTracer.context(() => {
if (_version !== cur[core.EffectTypeId]._V) {
- return core.dieMessage(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
+ console.warn(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
}
// @ts-expect-error
return this[cur._op](cur);
diff --git a/dist/esm/internal/fiberRuntime.js b/dist/esm/internal/fiberRuntime.js
index 122d06eaaf7f02f46794cbf829481a5ad8b74ffc..9e3cf0ec9b0f0fa45c334b3407b8fc05a5a6342f 100644
--- a/dist/esm/internal/fiberRuntime.js
+++ b/dist/esm/internal/fiberRuntime.js
@@ -1128,7 +1128,7 @@ export class FiberRuntime extends Effectable.Class {
// @ts-expect-error
cur = this.currentTracer.context(() => {
if (_version !== cur[core.EffectTypeId]._V) {
- return core.dieMessage(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
+ console.warn(`Cannot execute an Effect versioned ${cur[core.EffectTypeId]._V} with a Runtime of version ${version.getCurrentVersion()}`);
}
// @ts-expect-error
return this[cur._op](cur);
54 changes: 48 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"plugins": [
{
"name": "next"
},
{
"name": "@effect/language-service"
}
],
"paths": {
Expand Down