@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 286 lines 11.1 KB
Raw
1 import * as Bedrock from "@alchemy.run/aws/Bedrock";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 Layer from "effect/Layer";
8 import * as Redacted from "effect/Redacted";
9 import * as Stream from "effect/Stream";
10 import path from "pathe";
11
12 const main = path.resolve(import.meta.dirname, "handler.ts");
13
14 // Nova Micro via the us cross-region inference profile: the cheapest
15 // on-demand conversational model in the testing account (bare
16 // amazon.nova-micro-v1:0 rejects on-demand invocation).
17 const MODEL = "us.amazon.nova-micro-v1:0";
18
19 // Amazon's reranker — enabled in the testing account (us-west-2).
20 const RERANK_MODEL = "amazon.rerank-v1:0";
21
22 // CountTokens only supports a subset of models addressed by their BARE
23 // foundation-model id — Nova rejects it with "The provided model doesn't
24 // support counting tokens", as do cross-region inference-profile ids.
25 const COUNT_TOKENS_MODEL = "anthropic.claude-haiku-4-5-20251001-v1:0";
26
27 export class BedrockTestFunction extends Lambda.Function<Lambda.Function>()(
28 "BedrockTestFunction",
29 ) {}
30
31 export default BedrockTestFunction.make(
32 {
33 main,
34 functionUrl: true,
35 // Model inference (and agent orchestration especially) regularly
36 // exceeds Lambda's 3s default timeout.
37 timeout: Duration.seconds(60),
38 },
39 Effect.gen(function* () {
40 // An agent + alias to exercise InvokeAgent end-to-end. The alias
41 // snapshots the prepared DRAFT into version 1 on create.
42 const agent = yield* Bedrock.Agent("BindingsTestAgent", {
43 foundationModel: MODEL,
44 instruction: "You are a helpful assistant. Answer every question with one short sentence.",
45 // Long-term memory so GetAgentMemory / DeleteAgentMemory are callable.
46 memoryConfiguration: {
47 enabledMemoryTypes: ["SESSION_SUMMARY"],
48 storage: "30 days",
49 },
50 });
51 const alias = yield* Bedrock.AgentAlias("BindingsTestAlias", {
52 agentId: agent.agentId,
53 });
54
55 const converse = yield* Bedrock.Converse(MODEL);
56 const converseStream = yield* Bedrock.ConverseStream(MODEL);
57 const countTokens = yield* Bedrock.CountTokens(COUNT_TOKENS_MODEL);
58 const invokeAgent = yield* Bedrock.InvokeAgent(alias);
59 const getAgentMemory = yield* Bedrock.GetAgentMemory(alias);
60 const deleteAgentMemory = yield* Bedrock.DeleteAgentMemory(alias);
61 const invokeModel = yield* Bedrock.InvokeModel(MODEL);
62 const rerank = yield* Bedrock.Rerank(RERANK_MODEL);
63 const invokeModelStream = yield* Bedrock.InvokeModelWithResponseStream(MODEL);
64
65 return {
66 fetch: Effect.gen(function* () {
67 const request = yield* HttpServerRequest;
68 const url = new URL(request.originalUrl);
69 const pathname = url.pathname;
70
71 // Cheap readiness route — no Bedrock call.
72 if (request.method === "GET" && pathname === "/ping") {
73 return yield* HttpServerResponse.json({ ok: true });
74 }
75
76 if (request.method === "GET" && pathname === "/count-tokens") {
77 const result = yield* countTokens({
78 input: {
79 converse: {
80 messages: [{ role: "user", content: [{ text: "Say hello." }] }],
81 },
82 },
83 });
84 return yield* HttpServerResponse.json({
85 inputTokens: result.inputTokens,
86 });
87 }
88
89 if (request.method === "GET" && pathname === "/rerank") {
90 const region = yield* Effect.sync(() => process.env.AWS_REGION);
91 const result = yield* rerank({
92 queries: [{ type: "TEXT", textQuery: { text: "What is alchemy?" } }],
93 sources: [
94 "alchemy is an infrastructure-as-effects framework",
95 "bananas are yellow",
96 ].map((text) => ({
97 type: "INLINE",
98 inlineDocumentSource: {
99 type: "TEXT",
100 textDocument: { text },
101 },
102 })),
103 rerankingConfiguration: {
104 type: "BEDROCK_RERANKING_MODEL",
105 bedrockRerankingConfiguration: {
106 modelConfiguration: {
107 modelArn: `arn:aws:bedrock:${region}::foundation-model/${RERANK_MODEL}`,
108 },
109 },
110 },
111 });
112 return yield* HttpServerResponse.json({
113 results: result.results.map(({ index, relevanceScore }) => ({
114 index,
115 relevanceScore,
116 })),
117 });
118 }
119
120 if (request.method === "GET" && pathname === "/invoke-agent") {
121 const sessionId = yield* Effect.sync(() => crypto.randomUUID());
122 const result = yield* invokeAgent({
123 sessionId,
124 inputText: "What is the capital of France?",
125 });
126 const events = yield* Stream.runCollect(result.completion);
127 const decoder = new TextDecoder();
128 let chunkEvents = 0;
129 let text = "";
130 for (const event of events) {
131 const bytes = event.chunk?.bytes;
132 if (bytes !== undefined) {
133 chunkEvents += 1;
134 text += decoder.decode(Redacted.isRedacted(bytes) ? Redacted.value(bytes) : bytes, {
135 stream: true,
136 });
137 }
138 }
139 return yield* HttpServerResponse.json({
140 sessionId: result.sessionId,
141 chunkEvents,
142 text,
143 });
144 }
145
146 if (request.method === "GET" && pathname === "/agent-memory") {
147 // Read then clear memory for a fixed memory id. Summaries are
148 // generated asynchronously after a session ends, so on a fresh
149 // agent the read returns no contents — the route validates the
150 // IAM grants and wiring, not summary generation.
151 const memoryId = "alchemy-bindings-test-memory";
152 const memory = yield* getAgentMemory({
153 memoryType: "SESSION_SUMMARY",
154 memoryId,
155 maxItems: 10,
156 });
157 yield* deleteAgentMemory({ memoryId });
158 return yield* HttpServerResponse.json({
159 memoryContents: memory.memoryContents?.length ?? 0,
160 deleted: true,
161 });
162 }
163
164 if (request.method === "GET" && pathname === "/converse") {
165 const result = yield* converse({
166 system: [{ text: "You reply with exactly one word." }],
167 messages: [{ role: "user", content: [{ text: "Say hello." }] }],
168 inferenceConfig: { maxTokens: 64, temperature: 0 },
169 });
170 return yield* HttpServerResponse.json({
171 text: result.output.message.content.map((block) => block.text ?? "").join(""),
172 stopReason: result.stopReason,
173 outputTokens: result.usage.outputTokens,
174 });
175 }
176
177 if (request.method === "GET" && pathname === "/converse-stream") {
178 const result = yield* converseStream({
179 system: [{ text: "You reply with exactly one word." }],
180 messages: [{ role: "user", content: [{ text: "Say hello." }] }],
181 inferenceConfig: { maxTokens: 64, temperature: 0 },
182 });
183 const events = yield* Stream.runCollect(result.stream ?? Stream.empty);
184 let text = "";
185 let deltaEvents = 0;
186 let stopReason: string | undefined;
187 for (const event of events) {
188 if (event.contentBlockDelta?.delta.text !== undefined) {
189 text += event.contentBlockDelta.delta.text;
190 deltaEvents += 1;
191 }
192 if (event.messageStop !== undefined) {
193 stopReason = event.messageStop.stopReason;
194 }
195 }
196 return yield* HttpServerResponse.json({
197 text,
198 deltaEvents,
199 totalEvents: events.length,
200 stopReason,
201 });
202 }
203
204 if (request.method === "GET" && pathname === "/invoke-model-stream") {
205 const result = yield* invokeModelStream({
206 contentType: "application/json",
207 accept: "application/json",
208 // Raw Nova messages-v1 payload — no marshalling.
209 body: JSON.stringify({
210 system: [{ text: "You reply with exactly one word." }],
211 messages: [{ role: "user", content: [{ text: "Say hello." }] }],
212 inferenceConfig: { maxTokens: 64, temperature: 0 },
213 }),
214 });
215 const events = yield* Stream.runCollect(result.body);
216 const decoder = new TextDecoder();
217 let chunkEvents = 0;
218 let raw = "";
219 for (const event of events) {
220 const bytes = event.chunk?.bytes;
221 if (bytes !== undefined) {
222 chunkEvents += 1;
223 raw += decoder.decode(Redacted.isRedacted(bytes) ? Redacted.value(bytes) : bytes, {
224 stream: true,
225 });
226 }
227 }
228 // Each chunk is one JSON event (Nova emits messageStart,
229 // contentBlockDelta, ... as separate chunks); aggregate the text
230 // deltas out of the raw concatenated JSON events.
231 const text = [...raw.matchAll(/"text"\s*:\s*"([^"]*)"/g)].map((m) => m[1]).join("");
232 return yield* HttpServerResponse.json({
233 contentType: result.contentType,
234 chunkEvents,
235 text,
236 });
237 }
238
239 if (request.method === "GET" && pathname === "/invoke-model") {
240 const result = yield* invokeModel({
241 contentType: "application/json",
242 accept: "application/json",
243 // Raw Nova messages-v1 payload — InvokeModel does no marshalling.
244 body: JSON.stringify({
245 system: [{ text: "You reply with exactly one word." }],
246 messages: [{ role: "user", content: [{ text: "Say hello." }] }],
247 inferenceConfig: { maxTokens: 64, temperature: 0 },
248 }),
249 });
250 const raw = yield* Stream.mkString(Stream.decodeText(result.body));
251 const parsed = yield* Effect.try(
252 () =>
253 JSON.parse(raw) as {
254 output?: { message?: { content?: Array<{ text?: string }> } };
255 stopReason?: string;
256 },
257 );
258 return yield* HttpServerResponse.json({
259 contentType: result.contentType,
260 text: (parsed.output?.message?.content ?? []).map((block) => block.text ?? "").join(""),
261 stopReason: parsed.stopReason,
262 });
263 }
264
265 return yield* HttpServerResponse.json(
266 { error: "Not found", method: request.method, pathname },
267 { status: 404 },
268 );
269 }).pipe(Effect.orDie),
270 };
271 }).pipe(
272 Effect.provide(
273 Layer.mergeAll(
274 Bedrock.ConverseHttp,
275 Bedrock.ConverseStreamHttp,
276 Bedrock.CountTokensHttp,
277 Bedrock.DeleteAgentMemoryHttp,
278 Bedrock.GetAgentMemoryHttp,
279 Bedrock.InvokeAgentHttp,
280 Bedrock.InvokeModelHttp,
281 Bedrock.InvokeModelWithResponseStreamHttp,
282 Bedrock.RerankHttp,
283 ),
284 ),
285 ),
286 );