| 1 | import * as AI from "alchemy/AI"; |
| 2 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as S from "effect/Schema"; |
| 6 | |
| 7 | export const sql = AI.Parameter("sql", S.String)`The SQL query to execute.`; |
| 8 | |
| 9 | export class Sql extends (AI.Tool<Sql>()("sql")` |
| 10 | Execute a ${sql} query on the database and return the result.`) {} |
| 11 | |
| 12 | export const SqlDurableObjectLive = Layer.effect( |
| 13 | Sql, |
| 14 | Effect.gen(function* () { |
| 15 | const object = yield* Cloudflare.DurableObjectState; |
| 16 | |
| 17 | return Effect.gen(function* () { |
| 18 | // use environment dependencies when an instance of the environment is instantiated |
| 19 | yield* object.storage.sql.exec("CREATE TABLE IF NOT EXIST ..."); |
| 20 | |
| 21 | return ({ sql }) => |
| 22 | object.storage.sql |
| 23 | .exec(sql) |
| 24 | .pipe(Effect.flatMap((result) => result.toArray())); |
| 25 | }); |
| 26 | }), |
| 27 | ); |