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, 03:37 UTC
| 1 | import { |
| 2 | EventId, |
| 3 | OrchestrationV2RpcSchemas, |
| 4 | ThreadId, |
| 5 | TurnItemId, |
| 6 | type OrchestrationV2DomainEvent, |
| 7 | type OrchestrationV2ThreadStreamItem, |
| 8 | type OrchestrationV2TurnItem, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import * as DateTime from "effect/DateTime"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | import type * as RpcMessage from "effect/rpc/RpcMessage"; |
| 13 | import { describe, expect, it } from "vite-plus/test"; |
| 14 | |
| 15 | import { projectDomainEventForWire } from "./WireProjection.ts"; |
| 16 | |
| 17 | const OUTPUT_BYTES_PER_ROW = 8_192; |
| 18 | const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"); |
| 19 | const THREAD_ID = ThreadId.make("thread-perf"); |
| 20 | |
| 21 | const PRE_OMISSION_COMMAND_EVENT_RPC_JSON_BYTES = 8_790; |
| 22 | const MAX_PROJECTED_COMMAND_EVENT_RPC_JSON_BYTES = 1_024; |
| 23 | |
| 24 | const encodeThreadStreamItem = Schema.encodeSync( |
| 25 | Schema.toCodecJson(OrchestrationV2RpcSchemas.subscribeThread.output), |
| 26 | ); |
| 27 | |
| 28 | function encodedBytes(value: unknown): number { |
| 29 | return Buffer.byteLength(JSON.stringify(value), "utf8"); |
| 30 | } |
| 31 | |
| 32 | function encodedThreadStreamChunkBytes(value: OrchestrationV2ThreadStreamItem): number { |
| 33 | const response = { |
| 34 | _tag: "Chunk", |
| 35 | requestId: 0, |
| 36 | values: [encodeThreadStreamItem(value)], |
| 37 | } satisfies RpcMessage.ResponseChunkEncoded; |
| 38 | return encodedBytes(response); |
| 39 | } |
| 40 | |
| 41 | function makeCommandItem(index: number): OrchestrationV2TurnItem { |
| 42 | return { |
| 43 | id: TurnItemId.make(`item-${index}`), |
| 44 | type: "command_execution", |
| 45 | threadId: THREAD_ID, |
| 46 | runId: null, |
| 47 | nodeId: null, |
| 48 | providerThreadId: null, |
| 49 | providerTurnId: null, |
| 50 | nativeItemRef: null, |
| 51 | parentItemId: null, |
| 52 | ordinal: index + 1, |
| 53 | status: "completed", |
| 54 | title: `Command ${index}`, |
| 55 | input: `cmd-${index}`, |
| 56 | output: "x".repeat(OUTPUT_BYTES_PER_ROW), |
| 57 | exitCode: 0, |
| 58 | startedAt: NOW, |
| 59 | completedAt: NOW, |
| 60 | updatedAt: NOW, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | function makeTurnItemEvent(item: OrchestrationV2TurnItem): OrchestrationV2DomainEvent { |
| 65 | return { |
| 66 | id: EventId.make(`event-${item.id}`), |
| 67 | type: "turn-item.updated", |
| 68 | threadId: THREAD_ID, |
| 69 | occurredAt: NOW, |
| 70 | payload: item, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | describe("thread transport payload budget", () => { |
| 75 | it("omits command output from a live event without changing persisted source data", () => { |
| 76 | const sourceItem = makeCommandItem(0); |
| 77 | const sourceEvent = makeTurnItemEvent(sourceItem); |
| 78 | const projectedEvent = projectDomainEventForWire(sourceEvent); |
| 79 | const preOmissionStreamItem: OrchestrationV2ThreadStreamItem = { |
| 80 | kind: "event", |
| 81 | sequence: 1, |
| 82 | event: sourceEvent, |
| 83 | }; |
| 84 | const projectedStreamItem: OrchestrationV2ThreadStreamItem = { |
| 85 | kind: "event", |
| 86 | sequence: 1, |
| 87 | event: projectedEvent, |
| 88 | }; |
| 89 | |
| 90 | const preOmissionRpcJsonBytes = encodedThreadStreamChunkBytes(preOmissionStreamItem); |
| 91 | const projectedRpcJsonBytes = encodedThreadStreamChunkBytes(projectedStreamItem); |
| 92 | |
| 93 | expect(preOmissionRpcJsonBytes).toBe(PRE_OMISSION_COMMAND_EVENT_RPC_JSON_BYTES); |
| 94 | expect(projectedRpcJsonBytes).toBeLessThanOrEqual(MAX_PROJECTED_COMMAND_EVENT_RPC_JSON_BYTES); |
| 95 | expect(projectedRpcJsonBytes / preOmissionRpcJsonBytes).toBeLessThanOrEqual(0.1); |
| 96 | expect(sourceItem).toHaveProperty("output", "x".repeat(OUTPUT_BYTES_PER_ROW)); |
| 97 | expect(projectedEvent.payload).not.toHaveProperty("output"); |
| 98 | }); |
| 99 | }); |