|
1 | 1 | import * as Cause from "effect/Cause"; |
2 | 2 | import * as Clock from "effect/Clock"; |
3 | 3 | import * as Context from "effect/Context"; |
| 4 | +import * as Crypto from "effect/Crypto"; |
4 | 5 | import * as Deferred from "effect/Deferred"; |
5 | 6 | import * as Duration from "effect/Duration"; |
6 | 7 | import * as Effect from "effect/Effect"; |
@@ -270,14 +271,24 @@ export const make = ( |
270 | 271 | ): Effect.Effect< |
271 | 272 | AcpSessionRuntime["Service"], |
272 | 273 | EffectAcpErrors.AcpError, |
273 | | - ChildProcessSpawner.ChildProcessSpawner | Scope.Scope |
| 274 | + ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | Scope.Scope |
274 | 275 | > => |
275 | 276 | Effect.gen(function* () { |
| 277 | + const crypto = yield* Crypto.Crypto; |
276 | 278 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
277 | 279 | const runtimeScope = yield* Scope.Scope; |
278 | 280 | const eventQueue = yield* Queue.unbounded<AcpSessionRuntimeEvent>(); |
279 | 281 | const modeStateRef = yield* Ref.make<AcpSessionModeState | undefined>(undefined); |
280 | 282 | const toolCallsRef = yield* Ref.make(new Map<string, AcpToolCallState>()); |
| 283 | + const assistantItemRuntimeId = yield* crypto.randomUUIDv4.pipe( |
| 284 | + Effect.mapError( |
| 285 | + (cause) => |
| 286 | + new EffectAcpErrors.AcpTransportError({ |
| 287 | + detail: "Failed to generate an ACP assistant item runtime identifier.", |
| 288 | + cause, |
| 289 | + }), |
| 290 | + ), |
| 291 | + ); |
281 | 292 | const assistantSegmentRef = yield* Ref.make<AcpAssistantSegmentState>({ nextSegmentIndex: 0 }); |
282 | 293 | const configOptionsRef = yield* Ref.make(sessionConfigOptionsFromSetup(undefined)); |
283 | 294 | const startStateRef = yield* Ref.make<AcpStartState>({ _tag: "NotStarted" }); |
@@ -387,6 +398,7 @@ export const make = ( |
387 | 398 | modeStateRef, |
388 | 399 | toolCallsRef, |
389 | 400 | assistantSegmentRef, |
| 401 | + assistantItemRuntimeId, |
390 | 402 | params: notification, |
391 | 403 | }); |
392 | 404 | }), |
@@ -802,7 +814,7 @@ export const layer = ( |
802 | 814 | ): Layer.Layer< |
803 | 815 | AcpSessionRuntime, |
804 | 816 | EffectAcpErrors.AcpError, |
805 | | - ChildProcessSpawner.ChildProcessSpawner |
| 817 | + ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto |
806 | 818 | > => Layer.effect(AcpSessionRuntime, make(options)); |
807 | 819 |
|
808 | 820 | function sessionConfigOptionsFromSetup( |
@@ -834,12 +846,14 @@ const handleSessionUpdate = ({ |
834 | 846 | modeStateRef, |
835 | 847 | toolCallsRef, |
836 | 848 | assistantSegmentRef, |
| 849 | + assistantItemRuntimeId, |
837 | 850 | params, |
838 | 851 | }: { |
839 | 852 | readonly queue: Queue.Queue<AcpSessionRuntimeEvent>; |
840 | 853 | readonly modeStateRef: Ref.Ref<AcpSessionModeState | undefined>; |
841 | 854 | readonly toolCallsRef: Ref.Ref<Map<string, AcpToolCallState>>; |
842 | 855 | readonly assistantSegmentRef: Ref.Ref<AcpAssistantSegmentState>; |
| 856 | + readonly assistantItemRuntimeId: string; |
843 | 857 | readonly params: EffectAcpSchema.SessionNotification; |
844 | 858 | }): Effect.Effect<void> => |
845 | 859 | Effect.gen(function* () { |
@@ -887,6 +901,7 @@ const handleSessionUpdate = ({ |
887 | 901 | queue, |
888 | 902 | assistantSegmentRef, |
889 | 903 | sessionId: params.sessionId, |
| 904 | + assistantItemRuntimeId, |
890 | 905 | }); |
891 | 906 | yield* Queue.offer(queue, { |
892 | 907 | ...event, |
@@ -924,25 +939,27 @@ function shouldEmitToolCallUpdate( |
924 | 939 | return previous === undefined || previous.title !== next.title || previous.detail !== next.detail; |
925 | 940 | } |
926 | 941 |
|
927 | | -const assistantItemId = (sessionId: string, segmentIndex: number) => |
928 | | - `assistant:${sessionId}:segment:${segmentIndex}`; |
| 942 | +const assistantItemId = (sessionId: string, runtimeId: string, segmentIndex: number) => |
| 943 | + `assistant:${sessionId}:runtime:${runtimeId}:segment:${segmentIndex}`; |
929 | 944 |
|
930 | 945 | const ensureActiveAssistantSegment = ({ |
931 | 946 | queue, |
932 | 947 | assistantSegmentRef, |
933 | 948 | sessionId, |
| 949 | + assistantItemRuntimeId, |
934 | 950 | }: { |
935 | 951 | readonly queue: Queue.Queue<AcpSessionRuntimeEvent>; |
936 | 952 | readonly assistantSegmentRef: Ref.Ref<AcpAssistantSegmentState>; |
937 | 953 | readonly sessionId: string; |
| 954 | + readonly assistantItemRuntimeId: string; |
938 | 955 | }) => |
939 | 956 | Ref.modify<AcpAssistantSegmentState, EnsureActiveAssistantSegmentResult>( |
940 | 957 | assistantSegmentRef, |
941 | 958 | (current) => { |
942 | 959 | if (current.activeItemId) { |
943 | 960 | return [{ itemId: current.activeItemId }, current] as const; |
944 | 961 | } |
945 | | - const itemId = assistantItemId(sessionId, current.nextSegmentIndex); |
| 962 | + const itemId = assistantItemId(sessionId, assistantItemRuntimeId, current.nextSegmentIndex); |
946 | 963 | return [ |
947 | 964 | { |
948 | 965 | itemId, |
|
0 commit comments