Skip to content
Open
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: 9 additions & 6 deletions apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ const make = Effect.gen(function* () {
createdAt: string,
options?: {
readonly modelSelection?: ModelSelection;
readonly pendingTurnStart?: boolean;
},
) {
const thread = yield* resolveThread(threadId);
Expand Down Expand Up @@ -498,7 +499,10 @@ const make = Effect.gen(function* () {
threadId,
session: {
threadId,
status: mapProviderSessionStatusToOrchestrationStatus(session.status),
status:
options?.pendingTurnStart === true && session.status === "ready"
? "starting"
: mapProviderSessionStatusToOrchestrationStatus(session.status),
providerName: session.provider,
providerInstanceId: session.providerInstanceId,
runtimeMode: desiredRuntimeMode,
Expand Down Expand Up @@ -597,11 +601,10 @@ const make = Effect.gen(function* () {
new Error(`Thread '${input.threadId}' was not found in read model.`),
);
}
yield* ensureSessionForThread(
input.threadId,
input.createdAt,
input.modelSelection !== undefined ? { modelSelection: input.modelSelection } : {},
);
yield* ensureSessionForThread(input.threadId, input.createdAt, {
...(input.modelSelection !== undefined ? { modelSelection: input.modelSelection } : {}),
pendingTurnStart: true,
});
if (input.modelSelection !== undefined) {
threadModelSelections.set(input.threadId, input.modelSelection);
}
Expand Down
15 changes: 11 additions & 4 deletions apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,11 @@ const make = Effect.gen(function* () {
event.type === "turn.started" ||
event.type === "turn.completed"
) {
const hasPendingTurnStart = Option.isSome(
yield* projectionTurnRepository.getPendingTurnStartByThreadId({
threadId: thread.id,
}),
);
const nextActiveTurnId =
event.type === "turn.started"
? (eventTurnId ?? null)
Expand All @@ -1289,8 +1294,10 @@ const make = Effect.gen(function* () {
: activeTurnId;
const status = (() => {
switch (event.type) {
case "session.state.changed":
return orchestrationSessionStatusFromRuntimeState(event.payload.state);
case "session.state.changed": {
const runtimeStatus = orchestrationSessionStatusFromRuntimeState(event.payload.state);
return hasPendingTurnStart && runtimeStatus === "ready" ? "starting" : runtimeStatus;
}
case "turn.started":
return "running";
case "session.exited":
Expand All @@ -1302,8 +1309,8 @@ const make = Effect.gen(function* () {
case "session.started":
case "thread.started":
// Provider thread/session start notifications can arrive during an
// active turn; preserve turn-running state in that case.
return activeTurnId !== null ? "running" : "ready";
// active or pending turn; preserve that lifecycle state.
return activeTurnId !== null ? "running" : hasPendingTurnStart ? "starting" : "ready";
}
})();
const lastError =
Expand Down
Loading