Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

McpAppModelContext.ts

96 lines3.5 KB

1import type { ThreadId, TurnItemId } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as DateTime from "effect/DateTime";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Schema from "effect/Schema";
7import * as SqlClient from "effect/sql/SqlClient";
8​
9export const MCP_APP_MODEL_CONTEXT_MAX_BYTES = 16 * 1024;
10​
11export class McpAppModelContextError extends Schema.TaggedError<McpAppModelContextError>()(
12 "McpAppModelContextError",
13 { cause: Schema.optional(Schema.Defect()) },
14) {}
15​
16export interface McpAppModelContextEntry {
17 readonly itemId: string;
18 readonly server: string;
19 readonly tool: string;
20 readonly text: string;
22​
23export class McpAppModelContext extends Context.Service<
24 McpAppModelContext,
25 {
26 /** Replaces the app's context; empty text removes it. */
27 readonly set: (input: {
28 readonly threadId: ThreadId;
29 readonly itemId: TurnItemId;
30 readonly server: string;
31 readonly tool: string;
32 readonly text: string;
33 }) => Effect.Effect<void, McpAppModelContextError>;
34 readonly forThread: (
35 threadId: ThreadId,
36 ) => Effect.Effect<ReadonlyArray<McpAppModelContextEntry>, McpAppModelContextError>;
37 }
38>()("t3/mcpApps/McpAppModelContext") {}
39​
40export const layer = Layer.effect(
41 McpAppModelContext,
42 Effect.gen(function* () {
43 const sql = yield* SqlClient.SqlClient;
44 const fail = (cause: unknown) => new McpAppModelContextError({ cause });
45​
46 const set = Effect.fn("McpAppModelContext.set")(function* (input: {
47 readonly threadId: ThreadId;
48 readonly itemId: TurnItemId;
49 readonly server: string;
50 readonly tool: string;
51 readonly text: string;
52 }) {
53 if (input.text.trim() === "") {
54 yield* sql`
55 DELETE FROM mcp_app_model_context
56 WHERE thread_id = ${input.threadId} AND item_id = ${input.itemId}
57 `.pipe(Effect.mapError(fail));
58 return;
59 }
60 const updatedAt = DateTime.formatIso(yield* DateTime.now);
61 yield* sql`
62 INSERT INTO mcp_app_model_context (thread_id, item_id, server, tool, text, updated_at)
63 VALUES (${input.threadId}, ${input.itemId}, ${input.server}, ${input.tool}, ${input.text}, ${updatedAt})
64 ON CONFLICT(thread_id, item_id) DO UPDATE SET text = excluded.text, updated_at = excluded.updated_at
65 `.pipe(Effect.mapError(fail));
66 });
67​
68 const forThread = Effect.fn("McpAppModelContext.forThread")(function* (threadId: ThreadId) {
69 // The item can live in a thread this one was forked from, so it is
70 // matched by id alone; that thread rolling the run back later leaves the
71 // fork's copy in place.
72 return yield* sql<McpAppModelContextEntry>`
73 SELECT context.item_id AS "itemId", context.server, context.tool, context.text
74 FROM mcp_app_model_context AS context
75 JOIN orchestration_v2_projection_turn_items AS item
76 ON item.turn_item_id = context.item_id
77 LEFT JOIN orchestration_v2_projection_runs AS run
78 ON run.run_id = item.run_id
79 WHERE context.thread_id = ${threadId}
80 AND (
81 run.status IS NULL
82 OR run.status <> 'rolled_back'
83 OR run.thread_id <> context.thread_id
84 )
85 ORDER BY context.updated_at, context.item_id
86 `.pipe(Effect.mapError(fail));
87 });
88​
89 return McpAppModelContext.of({ set, forThread });
90 }),
91);
92​
93export const layerEmpty = Layer.succeed(
94 McpAppModelContext,
95 McpAppModelContext.of({ set: () => Effect.void, forThread: () => Effect.succeed([]) }),
96);