| ... | ... | @@ -1,8 +1,3 @@ |
| 1 | | import { |
| 2 | | RelayApi, |
| 3 | | type RelayAgentActivityPublishProofPayload, |
| 4 | | type RelayAgentActivityState, |
| 5 | | } from "@t3tools/contracts/relay"; |
| 6 | 1 | import type { |
| 7 | 2 | EnvironmentId, |
| 8 | 3 | OrchestrationEvent, |
| ... | ... | @@ -10,13 +5,18 @@ import type { |
| 10 | 5 | OrchestrationThreadShell, |
| 11 | 6 | ThreadId, |
| 12 | 7 | } from "@t3tools/contracts"; |
| 8 | import { |
| 9 | RelayApi, |
| 10 | type RelayAgentActivityPublishProofPayload, |
| 11 | type RelayAgentActivityState, |
| 12 | } from "@t3tools/contracts/relay"; |
| 13 | 13 | import { projectThreadAwareness } from "@t3tools/shared/agentAwareness"; |
| 14 | 14 | import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker"; |
| 15 | 15 | import { withRelayClientTracing } from "@t3tools/shared/relayTracing"; |
| 16 | 16 | import { |
| 17 | normalizeRelayIssuer, |
| 17 | 18 | RELAY_ACTIVITY_PUBLISH_TYP, |
| 18 | 19 | signRelayJwt, |
| 19 | | normalizeRelayIssuer, |
| 20 | 20 | } from "@t3tools/shared/relayJwt"; |
| 21 | 21 | import * as Cause from "effect/Cause"; |
| 22 | 22 | import * as Context from "effect/Context"; |
| ... | ... | @@ -28,31 +28,29 @@ import * as Option from "effect/Option"; |
| 28 | 28 | import * as Ref from "effect/Ref"; |
| 29 | 29 | import type * as Scope from "effect/Scope"; |
| 30 | 30 | import * as Stream from "effect/Stream"; |
| 31 | | import { FetchHttpClient } from "effect/unstable/http"; |
| 31 | import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient"; |
| 32 | 32 | import * as HttpClient from "effect/unstable/http/HttpClient"; |
| 33 | 33 | import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest"; |
| 34 | 34 | import * as HttpApiClient from "effect/unstable/httpapi/HttpApiClient"; |
| 35 | 35 | |
| 36 | 36 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 37 | | import { getOrCreateEnvironmentKeyPairFromSecretStore } from "../cloud/environmentKeys.ts"; |
| 38 | 37 | import { |
| 39 | 38 | PUBLISH_AGENT_ACTIVITY_SECRET, |
| 40 | 39 | RELAY_ENVIRONMENT_CREDENTIAL_SECRET, |
| 41 | 40 | RELAY_ISSUER_SECRET, |
| 42 | 41 | RELAY_URL_SECRET, |
| 43 | 42 | } from "../cloud/config.ts"; |
| 44 | | import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts"; |
| 45 | | import { OrchestrationEngineService } from "../orchestration/Services/OrchestrationEngine.ts"; |
| 46 | | import { ProjectionSnapshotQuery } from "../orchestration/Services/ProjectionSnapshotQuery.ts"; |
| 47 | | |
| 48 | | export interface AgentAwarenessRelayShape { |
| 49 | | readonly publishThread: (threadId: ThreadId) => Effect.Effect<void>; |
| 50 | | readonly start: () => Effect.Effect<void, never, Scope.Scope>; |
| 51 | | } |
| 43 | import { getOrCreateEnvironmentKeyPairFromSecretStore } from "../cloud/environmentKeys.ts"; |
| 44 | import * as ServerEnvironment from "../environment/Services/ServerEnvironment.ts"; |
| 45 | import * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.ts"; |
| 46 | import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts"; |
| 52 | 47 | |
| 53 | 48 | export class AgentAwarenessRelay extends Context.Service< |
| 54 | 49 | AgentAwarenessRelay, |
| 55 | | AgentAwarenessRelayShape |
| 50 | { |
| 51 | readonly publishThread: (threadId: ThreadId) => Effect.Effect<void>; |
| 52 | readonly start: () => Effect.Effect<void, never, Scope.Scope>; |
| 53 | } |
| 56 | 54 | >()("t3/relay/AgentAwarenessRelay") {} |
| 57 | 55 | |
| 58 | 56 | export function eventThreadId(event: OrchestrationEvent): ThreadId | null { |
| ... | ... | @@ -265,11 +263,11 @@ export function resolveAgentAwarenessRelayActiveThreadIds(input: { |
| 265 | 263 | .map((thread) => thread.id); |
| 266 | 264 | } |
| 267 | 265 | |
| 268 | | const make = Effect.gen(function* () { |
| 266 | export const make = Effect.gen(function* () { |
| 269 | 267 | const secrets = yield* ServerSecretStore.ServerSecretStore; |
| 270 | | const serverEnvironment = yield* ServerEnvironment; |
| 271 | | const snapshotQuery = yield* ProjectionSnapshotQuery; |
| 272 | | const orchestrationEngine = yield* OrchestrationEngineService; |
| 268 | const serverEnvironment = yield* ServerEnvironment.ServerEnvironment; |
| 269 | const snapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery; |
| 270 | const orchestrationEngine = yield* OrchestrationEngine.OrchestrationEngineService; |
| 273 | 271 | const crypto = yield* Crypto.Crypto; |
| 274 | 272 | const cloudLinkKeyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets); |
| 275 | 273 | const activeSnapshotPublishedRef = yield* Ref.make(false); |
| ... | ... | @@ -417,7 +415,7 @@ const make = Effect.gen(function* () { |
| 417 | 415 | }); |
| 418 | 416 | }); |
| 419 | 417 | |
| 420 | | const publishThread: AgentAwarenessRelayShape["publishThread"] = (threadId) => |
| 418 | const publishThread: AgentAwarenessRelay["Service"]["publishThread"] = (threadId) => |
| 421 | 419 | publishThreadUnsafe(threadId).pipe( |
| 422 | 420 | Effect.catchCause((cause) => { |
| 423 | 421 | return Effect.logWarning("agent activity publish failed", { |
| ... | ... | @@ -480,7 +478,7 @@ const make = Effect.gen(function* () { |
| 480 | 478 | |
| 481 | 479 | const worker = yield* makeDrainableWorker(publishThread); |
| 482 | 480 | |
| 483 | | const start: AgentAwarenessRelayShape["start"] = Effect.fn("AgentAwarenessRelay.start")( |
| 481 | const start: AgentAwarenessRelay["Service"]["start"] = Effect.fn("AgentAwarenessRelay.start")( |
| 484 | 482 | function* () { |
| 485 | 483 | const [relayConfig, publishEnabled] = yield* Effect.all([ |
| 486 | 484 | readRelayConfig.pipe(Effect.orElseSucceed(() => null)), |
| ... | ... | @@ -536,10 +534,10 @@ const make = Effect.gen(function* () { |
| 536 | 534 | }, |
| 537 | 535 | ); |
| 538 | 536 | |
| 539 | | return { |
| 537 | return AgentAwarenessRelay.of({ |
| 540 | 538 | publishThread, |
| 541 | 539 | start, |
| 542 | | } satisfies AgentAwarenessRelayShape; |
| 540 | }); |
| 543 | 541 | }); |
| 544 | 542 | |
| 545 | 543 | export const layer = Layer.effect(AgentAwarenessRelay, make); |