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, 04:45 UTC
| 1 | import type { ThreadId, TurnItemId } from "@t3tools/contracts"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 4 | import * as DateTime from "effect/DateTime"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import * as SqlClient from "effect/sql/SqlClient"; |
| 9 | |
| 10 | export const MCP_APP_MODEL_CONTEXT_MAX_BYTES = 16 * 1024; |
| 11 | |
| 12 | export class McpAppModelContextError extends Schema.TaggedError<McpAppModelContextError>()( |
| 13 | "McpAppModelContextError", |
| 14 | { cause: Schema.optional(Schema.Defect()) }, |
| 15 | ) {} |
| 16 | |
| 17 | export interface McpAppModelContextEntry { |
| 18 | readonly itemId: string; |
| 19 | readonly server: string; |
| 20 | readonly tool: string; |
| 21 | readonly text: string; |
| 22 | } |
| 23 | |
| 24 | export class McpAppModelContext extends Context.Service< |
| 25 | McpAppModelContext, |
| 26 | { |
| 27 | /** Empty text removes the app's context. */ |
| 28 | readonly set: (input: { |
| 29 | readonly threadId: ThreadId; |
| 30 | readonly itemId: TurnItemId; |
| 31 | readonly server: string; |
| 32 | readonly tool: string; |
| 33 | readonly text: string; |
| 34 | }) => Effect.Effect<void, McpAppModelContextError>; |
| 35 | readonly forThread: ( |
| 36 | threadId: ThreadId, |
| 37 | ) => Effect.Effect<ReadonlyArray<McpAppModelContextEntry>, McpAppModelContextError>; |
| 38 | } |
| 39 | >()("t3/mcpApps/McpAppModelContext") {} |
| 40 | |
| 41 | /** The thread's notes as a provider turn or steer sends them; a failed read sends none. */ |
| 42 | export const forProvider = Effect.fn("McpAppModelContext.forProvider")(function* ( |
| 43 | modelContext: McpAppModelContext["Service"], |
| 44 | threadId: ThreadId, |
| 45 | ) { |
| 46 | const entries = yield* modelContext |
| 47 | .forThread(threadId) |
| 48 | .pipe( |
| 49 | Effect.catch((cause) => |
| 50 | Effect.logWarning("Failed to read MCP app model context.", { cause }).pipe( |
| 51 | Effect.as([] as ReadonlyArray<McpAppModelContextEntry>), |
| 52 | ), |
| 53 | ), |
| 54 | ); |
| 55 | return entries.map((entry) => ({ |
| 56 | // The item id alone is unique and needs no escaping; server and |
| 57 | // tool names are free text that would break the tag Codex wraps |
| 58 | // the context in. |
| 59 | key: `mcp_app_${entry.itemId.replace(/[^\w.-]/g, "_")}`, |
| 60 | text: entry.text, |
| 61 | })); |
| 62 | }); |
| 63 | |
| 64 | export const layer = Layer.effect( |
| 65 | McpAppModelContext, |
| 66 | Effect.gen(function* () { |
| 67 | const sql = yield* SqlClient.SqlClient; |
| 68 | const fail = (cause: unknown) => new McpAppModelContextError({ cause }); |
| 69 | |
| 70 | const set = Effect.fn("McpAppModelContext.set")(function* (input: { |
| 71 | readonly threadId: ThreadId; |
| 72 | readonly itemId: TurnItemId; |
| 73 | readonly server: string; |
| 74 | readonly tool: string; |
| 75 | readonly text: string; |
| 76 | }) { |
| 77 | if (input.text.trim() === "") { |
| 78 | yield* sql` |
| 79 | DELETE FROM mcp_app_model_context |
| 80 | WHERE thread_id = ${input.threadId} AND item_id = ${input.itemId} |
| 81 | `.pipe(Effect.mapError(fail)); |
| 82 | return; |
| 83 | } |
| 84 | const updatedAt = DateTime.formatIso(yield* DateTime.now); |
| 85 | yield* sql` |
| 86 | INSERT INTO mcp_app_model_context (thread_id, item_id, server, tool, text, updated_at) |
| 87 | VALUES (${input.threadId}, ${input.itemId}, ${input.server}, ${input.tool}, ${input.text}, ${updatedAt}) |
| 88 | ON CONFLICT(thread_id, item_id) DO UPDATE SET text = excluded.text, updated_at = excluded.updated_at |
| 89 | `.pipe(Effect.mapError(fail)); |
| 90 | }); |
| 91 | |
| 92 | const forThread = Effect.fn("McpAppModelContext.forThread")(function* (threadId: ThreadId) { |
| 93 | // Match by id across fork lineage; later source rollbacks leave the fork's copy intact. |
| 94 | return yield* sql<McpAppModelContextEntry>` |
| 95 | SELECT context.item_id AS "itemId", context.server, context.tool, context.text |
| 96 | FROM mcp_app_model_context AS context |
| 97 | JOIN orchestration_v2_projection_turn_items AS item |
| 98 | ON item.turn_item_id = context.item_id |
| 99 | LEFT JOIN orchestration_v2_projection_runs AS run |
| 100 | ON run.run_id = item.run_id |
| 101 | WHERE context.thread_id = ${threadId} |
| 102 | AND ( |
| 103 | run.status IS NULL |
| 104 | OR run.status <> 'rolled_back' |
| 105 | OR run.thread_id <> context.thread_id |
| 106 | ) |
| 107 | ORDER BY context.updated_at, context.item_id |
| 108 | `.pipe(NodeSqliteClient.readOnly, Effect.mapError(fail)); |
| 109 | }); |
| 110 | |
| 111 | return McpAppModelContext.of({ set, forThread }); |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | export const layerEmpty = Layer.succeed( |
| 116 | McpAppModelContext, |
| 117 | McpAppModelContext.of({ |
| 118 | set: Effect.fn("McpAppModelContext.set")(function () { |
| 119 | return Effect.void; |
| 120 | }), |
| 121 | forThread: Effect.fn("McpAppModelContext.forThread")(function () { |
| 122 | return Effect.succeed([]); |
| 123 | }), |
| 124 | }), |
| 125 | ); |