The best way to control Claude Code, Codex and more from any computer or phone

main
ts 101 lines 4.21 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { LanguageModel as AiLanguageModel, Tool, Toolkit } from "effect/ai";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Schema from "effect/Schema";
8 import * as Stream from "effect/Stream";
9
10 // Amazon Nova Micro through the us cross-region inference profile — cheap
11 // and fast. Any conversational Bedrock model works here (Claude, Llama,
12 // Mistral, ...); enable it under "Model access" in the Bedrock console.
13 const MODEL = "us.amazon.nova-micro-v1:0";
14
15 const GetWeather = Tool.make("get_weather", {
16 description: "Get the current weather for a city.",
17 parameters: Schema.Struct({ city: Schema.String }),
18 success: Schema.Struct({
19 city: Schema.String,
20 temperatureF: Schema.Number,
21 condition: Schema.String,
22 }),
23 });
24
25 const WeatherToolkit = Toolkit.make(GetWeather);
26
27 const WeatherToolkitLayer = WeatherToolkit.toLayer({
28 get_weather: ({ city }) => Effect.succeed({ city, temperatureF: 72, condition: "sunny" }),
29 });
30
31 export default class ChatFunction extends AWS.Lambda.Function<ChatFunction>()(
32 "ChatFunction",
33 { main: import.meta.url, functionUrl: true, timeout: Duration.seconds(60) },
34 Effect.gen(function* () {
35 // Init: bind the model. This grants the Function `bedrock:InvokeModel`
36 // and `bedrock:InvokeModelWithResponseStream` scoped to exactly MODEL
37 // and returns an Effect AI `LanguageModel` Layer.
38 const model = yield* AWS.Bedrock.LanguageModel(MODEL, {
39 parameters: { maxTokens: 1024, temperature: 0.7 },
40 });
41
42 return {
43 fetch: Effect.gen(function* () {
44 const request = yield* HttpServerRequest;
45 const url = new URL(request.originalUrl);
46 const prompt = url.searchParams.get("prompt") ?? "Say hello.";
47
48 // GET /stream?prompt=... — stream the answer as SSE parts.
49 if (url.pathname === "/stream") {
50 const parts = yield* Stream.runCollect(AiLanguageModel.streamText({ prompt }));
51 const sse = [...parts].map((part) => `data: ${JSON.stringify(part)}\n\n`).join("");
52 return HttpServerResponse.text(sse, { headers: { "content-type": "text/event-stream" } });
53 }
54
55 // GET /weather?prompt=... — let the model call a typed tool.
56 if (url.pathname === "/weather") {
57 const response = yield* AiLanguageModel.generateText({
58 prompt,
59 toolkit: WeatherToolkit,
60 }).pipe(Effect.provide(WeatherToolkitLayer));
61 return yield* HttpServerResponse.json({
62 text: response.text,
63 toolCalls: response.toolCalls.map((call) => ({ name: call.name, params: call.params })),
64 toolResults: response.toolResults.map((result) => ({
65 name: result.name,
66 result: result.result,
67 })),
68 });
69 }
70
71 // GET /?prompt=...&temperature=0.2&maxTokens=64 — single-shot text
72 // generation. IAM access to the model is bound at deploy time, but
73 // inference parameters are a per-request decision:
74 // `withModelParameters` overrides the binding's defaults for just
75 // this call.
76 const maxTokens = url.searchParams.get("maxTokens");
77 const temperature = url.searchParams.get("temperature");
78 const response = yield* AiLanguageModel.generateText({ prompt }).pipe(
79 AWS.Bedrock.withModelParameters({
80 ...(maxTokens !== null ? { maxTokens: Number(maxTokens) } : {}),
81 ...(temperature !== null ? { temperature: Number(temperature) } : {}),
82 }),
83 );
84 return yield* HttpServerResponse.json({
85 text: response.text,
86 finishReason: response.finishReason,
87 usage: {
88 inputTokens: response.usage.inputTokens.total,
89 outputTokens: response.usage.outputTokens.total,
90 },
91 });
92 }).pipe(
93 Effect.catchTag("AiError", (error) =>
94 HttpServerResponse.json({ error: error.message }, { status: 500 }),
95 ),
96 Effect.provide(model),
97 Effect.orDie,
98 ),
99 };
100 }).pipe(Effect.provide(AWS.Bedrock.LanguageModelHttp)),
101 ) {}