Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/server/src/kin/chat/KinChatHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ export class KinChatHub extends Context.Service<
never,
Scope.Scope
>;
/** Every organization's committed events, unfiltered. Server consumers only, never a client. */
readonly subscribeAll: Effect.Effect<Stream.Stream<KinChatHubEvent>, never, Scope.Scope>;
}
>()("t3/kin/chat/KinChatHub") {}

const make = Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
const repo = yield* Repository.KinChatRepository;
const hubs = new Map<KinOrgId, PubSub.PubSub<KinChatHubEvent>>();
let all: PubSub.PubSub<KinChatHubEvent> | undefined;
yield* Effect.addFinalizer(() =>
Effect.forEach(hubs.values(), PubSub.shutdown, { discard: true }),
Effect.forEach([...hubs.values(), ...(all === undefined ? [] : [all])], PubSub.shutdown, {
discard: true,
}),
);
const subscribe = Effect.fn("KinChatHub.subscribe")(function* (orgId: KinOrgId) {
let hub = hubs.get(orgId);
Expand All @@ -70,9 +75,17 @@ const make = Effect.gen(function* () {
Effect.gen(function* () {
const hub = hubs.get(event.orgId);
if (hub !== undefined) yield* PubSub.publish(hub, event);
if (all !== undefined) yield* PubSub.publish(all, event);
}),
);
}),
subscribeAll: Effect.gen(function* () {
if (all === undefined) {
const created = yield* PubSub.unbounded<KinChatHubEvent>();
all ??= created;
}
return Stream.fromEffectRepeat(PubSub.take(yield* PubSub.subscribe(all)));
}).pipe(Effect.withSpan("KinChatHub.subscribeAll")),
subscribeForViewer: Effect.fn("KinChatHub.subscribeForViewer")(function* (identity) {
const subscription = yield* subscribe(identity.orgId);
return Stream.fromEffectRepeat(PubSub.take(subscription)).pipe(
Expand Down
33 changes: 32 additions & 1 deletion apps/server/src/kin/floor/KinFloor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ export class KinFloor extends Context.Service<
message: KinMessage,
chat: KinChat,
) => Effect.Effect<FloorDecision | null, PersistenceSqlError>;
/**
* Re-arms the agent's unused direct-chat grant on each open run one of `messageIds` opened,
* so a wake-up that starts after the TTL can still reply once. It never issues a grant.
* `until` (the wake-up's own deadline) replaces the default TTL from now.
*/
readonly renew: (input: {
readonly orgId: KinOrgId;
readonly agentId: KinAgentId;
readonly messageIds: ReadonlyArray<KinMessageId>;
readonly until?: number;
}) => Effect.Effect<void, PersistenceSqlError>;
readonly authorize: (
identity: AgentIdentity,
chat: KinChat,
Expand Down Expand Up @@ -168,6 +179,26 @@ const make = Effect.gen(function* () {
Effect.mapError(toPersistenceSqlError("KinFloor.open")),
);

const renew = Effect.fn("KinFloor.renew")(
function* (input: {
readonly orgId: KinOrgId;
readonly agentId: KinAgentId;
readonly messageIds: ReadonlyArray<KinMessageId>;
readonly until?: number;
}) {
if (input.messageIds.length === 0) return;
const expiresAt = input.until ?? (yield* Clock.currentTimeMillis) + FLOOR_GRANT_TTL_MS;
// A new human message closes the run, and a reply consumes the grant, so neither re-arms.
yield* sql`UPDATE kin_floor_grants SET expires_at = MAX(expires_at, ${expiresAt})
WHERE org_id = ${input.orgId} AND agent_id = ${input.agentId} AND consumed_at IS NULL
AND run_id IN (SELECT r.run_id FROM kin_floor_runs r
JOIN kin_chats c ON c.org_id = r.org_id AND c.chat_id = r.chat_id AND c.kind = 'direct'
WHERE r.org_id = ${input.orgId} AND r.state = 'open'
AND r.opened_by IN ${sql.in(input.messageIds)})`;
},
Effect.mapError(toPersistenceSqlError("KinFloor.renew")),
);

const noticeOnly = Effect.fn("KinFloor.noticeOnly")(
function* (identity: AgentIdentity) {
// Notice authority comes from the running wake-up's stored events, never tool input.
Expand Down Expand Up @@ -308,6 +339,6 @@ const make = Effect.gen(function* () {
},
Effect.mapError(toPersistenceSqlError("KinFloor.recordNotice")),
);
return KinFloor.of({ open, authorize, noticeOnly, admitNotice, recordNotice });
return KinFloor.of({ open, renew, authorize, noticeOnly, admitNotice, recordNotice });
});
export const layer = Layer.effect(KinFloor, make);
Loading