Expose V2 thread workflows on mobile
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
2fde64e80f
commit
beab58b45b
16 files changed
+1025
-218
No files matched your search
@@ -9,6 +9,7 @@ import {
|
||||
type ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
|
||||
import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows";
|
||||
import { resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList";
|
||||
import {
|
||||
createContext,
|
||||
@@ -1191,17 +1192,10 @@ function AssistantForkButton({
|
||||
sourceThreadId: projectedItem.sourceThreadId,
|
||||
sourceItemId: projectedItem.sourceItemId,
|
||||
});
|
||||
const capabilities = support.providerSession?.capabilities;
|
||||
const canForkNatively =
|
||||
capabilities?.threads.canForkThread === true &&
|
||||
capabilities.threads.canForkFromTurn === true &&
|
||||
capabilities.identity.nativeThreadIds === "strong";
|
||||
const canFork =
|
||||
projectedItem.item.runId !== null &&
|
||||
projectedItem.item.status === "completed" &&
|
||||
(capabilities === undefined ||
|
||||
canForkNatively ||
|
||||
capabilities.context.supportsFullThreadHandoff === true);
|
||||
const canFork = canForkProjectedAssistantItem({
|
||||
projectedItem,
|
||||
capabilities: support.providerSession?.capabilities,
|
||||
});
|
||||
|
||||
if (!canFork || projectedItem.item.runId === null) return null;
|
||||
const runId = projectedItem.item.runId;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
||||
import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows";
|
||||
import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
|
||||
import { ArrowDownIcon, ArrowUpIcon, CornerUpRightIcon, ListOrderedIcon } from "lucide-react";
|
||||
import { useMemo, useState } from "react";
|
||||
@@ -18,31 +19,12 @@ export function QueuedRunsControl(props: {
|
||||
const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun);
|
||||
const promote = useAtomCommand(threadEnvironment.promoteQueuedRun);
|
||||
const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
|
||||
const queued = useMemo(
|
||||
() =>
|
||||
(projection?.runs ?? [])
|
||||
.filter((run) => run.status === "queued")
|
||||
.toSorted(
|
||||
(left, right) =>
|
||||
(left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal) ||
|
||||
left.ordinal - right.ordinal,
|
||||
)
|
||||
.map((run) => ({
|
||||
run,
|
||||
text:
|
||||
projection?.messages.find((message) => message.id === run.userMessageId)?.text ??
|
||||
"Queued message",
|
||||
})),
|
||||
const workflow = useMemo(
|
||||
() => (projection ? deriveThreadQueueWorkflowState(projection) : null),
|
||||
[projection],
|
||||
);
|
||||
const activeRun =
|
||||
projection?.runs.findLast(
|
||||
(run) =>
|
||||
run.status === "preparing" ||
|
||||
run.status === "starting" ||
|
||||
run.status === "running" ||
|
||||
run.status === "waiting",
|
||||
) ?? null;
|
||||
const queued = workflow?.queuedRuns ?? [];
|
||||
const activeRun = workflow?.activeRun ?? null;
|
||||
|
||||
if (queued.length === 0) return null;
|
||||
|
||||
@@ -83,7 +65,7 @@ export function QueuedRunsControl(props: {
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
aria-label="Move queued message up"
|
||||
disabled={busyRunId !== null || index === 0}
|
||||
disabled={busyRunId !== null || !workflow?.canReorder || index === 0}
|
||||
onClick={() => void move(run.id, queued[index - 1]?.run.id ?? null)}
|
||||
>
|
||||
<ArrowUpIcon className="size-3" />
|
||||
@@ -92,7 +74,7 @@ export function QueuedRunsControl(props: {
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
aria-label="Move queued message down"
|
||||
disabled={busyRunId !== null || index === queued.length - 1}
|
||||
disabled={busyRunId !== null || !workflow?.canReorder || index === queued.length - 1}
|
||||
onClick={() => void move(run.id, queued[index + 2]?.run.id ?? null)}
|
||||
>
|
||||
<ArrowDownIcon className="size-3" />
|
||||
@@ -100,7 +82,7 @@ export function QueuedRunsControl(props: {
|
||||
<Button
|
||||
size="xs"
|
||||
variant="outline"
|
||||
disabled={busyRunId !== null || activeRun === null}
|
||||
disabled={busyRunId !== null || !workflow?.canPromoteToSteer}
|
||||
title={activeRun === null ? "There is no active run to steer" : "Promote to steer"}
|
||||
onClick={() => void steer(run.id)}
|
||||
>
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
||||
import {
|
||||
deriveThreadRelationshipGraph,
|
||||
immediateThreadRelationships,
|
||||
resolveMergeBackTargetThreadId,
|
||||
type ThreadRelationshipEdge,
|
||||
} from "@t3tools/client-runtime/state/thread-relationships";
|
||||
import { canDetachThreadProviderSession } from "@t3tools/client-runtime/state/thread-workflows";
|
||||
import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
import {
|
||||
@@ -13,12 +20,6 @@ import {
|
||||
import { useState } from "react";
|
||||
|
||||
import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState";
|
||||
import {
|
||||
deriveThreadRelationshipGraph,
|
||||
immediateThreadRelationships,
|
||||
resolveMergeBackTargetThreadId,
|
||||
type ThreadRelationshipEdge,
|
||||
} from "../../lib/threadRelationships";
|
||||
import { buildThreadRouteParams } from "../../threadRoutes";
|
||||
import { useThreadProjection, useThreadShells } from "../../state/entities";
|
||||
import { threadEnvironment } from "../../state/threads";
|
||||
@@ -91,10 +92,7 @@ export function ThreadRelationshipsPanel(props: {
|
||||
Number(left.threadId === mergeTargetThreadId),
|
||||
);
|
||||
const canMerge = mergeTargetThreadId !== null && latestCompletedRun !== null;
|
||||
const canDetach =
|
||||
projection?.providerSessions.some(
|
||||
(session) => session.status !== "stopped" && session.status !== "error",
|
||||
) ?? false;
|
||||
const canDetach = projection ? canDetachThreadProviderSession(projection) : false;
|
||||
|
||||
if (relationshipRows.length === 0) {
|
||||
return null;
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { ThreadId } from "@t3tools/contracts";
|
||||
|
||||
import {
|
||||
deriveThreadRelationshipGraph,
|
||||
immediateThreadRelationships,
|
||||
relatedThreadIds,
|
||||
resolveMergeBackTargetThreadId,
|
||||
walkThreadRelationships,
|
||||
} from "./threadRelationships";
|
||||
|
||||
describe("thread relationships", () => {
|
||||
it("keeps missing parents and cycles navigable without recursive traversal", () => {
|
||||
const root = ThreadId.make("thread-root");
|
||||
const child = ThreadId.make("thread-child");
|
||||
const missing = ThreadId.make("thread-missing");
|
||||
const graph = deriveThreadRelationshipGraph({
|
||||
threads: [
|
||||
{
|
||||
id: root,
|
||||
title: "Root",
|
||||
status: "completed",
|
||||
forkedFrom: { type: "run", threadId: child, runId: "run-cycle" },
|
||||
lineage: { rootThreadId: root, parentThreadId: child, relationshipToParent: "fork" },
|
||||
},
|
||||
{
|
||||
id: child,
|
||||
title: "Child",
|
||||
status: "completed",
|
||||
forkedFrom: { type: "run", threadId: missing, runId: "run-missing" },
|
||||
lineage: { rootThreadId: root, parentThreadId: missing, relationshipToParent: "fork" },
|
||||
},
|
||||
] as never,
|
||||
projection: null,
|
||||
});
|
||||
|
||||
expect(graph.nodes.get(missing)?.missing).toBe(true);
|
||||
expect(relatedThreadIds(graph, root)).toEqual([child]);
|
||||
expect(relatedThreadIds(graph, child)).toEqual([root, missing]);
|
||||
expect(
|
||||
walkThreadRelationships(graph, root).map(({ threadId, depth }) => [threadId, depth]),
|
||||
).toEqual([
|
||||
[child, 1],
|
||||
[missing, 2],
|
||||
]);
|
||||
expect(immediateThreadRelationships(graph, root).map(({ threadId }) => threadId)).toEqual([
|
||||
child,
|
||||
]);
|
||||
});
|
||||
|
||||
it("resolves merge-back only for forks and prefers the recorded fork source", () => {
|
||||
const source = ThreadId.make("thread-source");
|
||||
const fallbackParent = ThreadId.make("thread-parent");
|
||||
const fork = ThreadId.make("thread-fork");
|
||||
|
||||
expect(
|
||||
resolveMergeBackTargetThreadId({
|
||||
thread: {
|
||||
id: fork,
|
||||
forkedFrom: { type: "run", threadId: source, runId: "run-source" },
|
||||
lineage: {
|
||||
rootThreadId: source,
|
||||
parentThreadId: fallbackParent,
|
||||
relationshipToParent: "fork",
|
||||
},
|
||||
},
|
||||
} as never),
|
||||
).toBe(source);
|
||||
expect(
|
||||
resolveMergeBackTargetThreadId({
|
||||
thread: {
|
||||
id: fork,
|
||||
forkedFrom: null,
|
||||
lineage: {
|
||||
rootThreadId: source,
|
||||
parentThreadId: fallbackParent,
|
||||
relationshipToParent: "fork",
|
||||
},
|
||||
},
|
||||
} as never),
|
||||
).toBe(fallbackParent);
|
||||
expect(
|
||||
resolveMergeBackTargetThreadId({
|
||||
thread: {
|
||||
id: fork,
|
||||
forkedFrom: null,
|
||||
lineage: {
|
||||
rootThreadId: source,
|
||||
parentThreadId: fallbackParent,
|
||||
relationshipToParent: "subagent",
|
||||
},
|
||||
},
|
||||
} as never),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -1,170 +0,0 @@
|
||||
import type {
|
||||
OrchestrationV2ThreadProjection,
|
||||
OrchestrationV2ThreadShell,
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export type ThreadRelationshipKind = "parent" | "fork" | "subagent" | "transfer";
|
||||
|
||||
export interface ThreadRelationshipNode {
|
||||
readonly threadId: ThreadId;
|
||||
readonly thread: OrchestrationV2ThreadShell | null;
|
||||
readonly missing: boolean;
|
||||
}
|
||||
|
||||
export interface ThreadRelationshipEdge {
|
||||
readonly sourceThreadId: ThreadId;
|
||||
readonly targetThreadId: ThreadId;
|
||||
readonly kind: ThreadRelationshipKind;
|
||||
readonly status: string | null;
|
||||
}
|
||||
|
||||
export interface ThreadRelationshipGraph {
|
||||
readonly nodes: ReadonlyMap<ThreadId, ThreadRelationshipNode>;
|
||||
readonly edges: ReadonlyArray<ThreadRelationshipEdge>;
|
||||
}
|
||||
|
||||
export interface ThreadRelationshipWalkRow {
|
||||
readonly threadId: ThreadId;
|
||||
readonly fromThreadId: ThreadId;
|
||||
readonly depth: number;
|
||||
readonly edge: ThreadRelationshipEdge;
|
||||
}
|
||||
|
||||
export function resolveMergeBackTargetThreadId(
|
||||
projection: Pick<OrchestrationV2ThreadProjection, "thread"> | null,
|
||||
): ThreadId | null {
|
||||
if (projection?.thread.lineage.relationshipToParent !== "fork") return null;
|
||||
return projection.thread.forkedFrom?.type === "run"
|
||||
? projection.thread.forkedFrom.threadId
|
||||
: projection.thread.lineage.parentThreadId;
|
||||
}
|
||||
|
||||
function edgeKey(edge: ThreadRelationshipEdge): string {
|
||||
return `${edge.sourceThreadId}\u001f${edge.targetThreadId}\u001f${edge.kind}`;
|
||||
}
|
||||
|
||||
export function deriveThreadRelationshipGraph(input: {
|
||||
readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
|
||||
readonly projection: OrchestrationV2ThreadProjection | null;
|
||||
}): ThreadRelationshipGraph {
|
||||
const nodes = new Map<ThreadId, ThreadRelationshipNode>(
|
||||
input.threads.map(
|
||||
(thread) => [thread.id, { threadId: thread.id, thread, missing: false }] as const,
|
||||
),
|
||||
);
|
||||
const edgesByKey = new Map<string, ThreadRelationshipEdge>();
|
||||
const ensureNode = (threadId: ThreadId) => {
|
||||
if (!nodes.has(threadId)) {
|
||||
nodes.set(threadId, { threadId, thread: null, missing: true });
|
||||
}
|
||||
};
|
||||
const addEdge = (edge: ThreadRelationshipEdge) => {
|
||||
ensureNode(edge.sourceThreadId);
|
||||
ensureNode(edge.targetThreadId);
|
||||
edgesByKey.set(edgeKey(edge), edge);
|
||||
};
|
||||
|
||||
for (const thread of input.threads) {
|
||||
const parentThreadId =
|
||||
thread.forkedFrom?.type === "run"
|
||||
? thread.forkedFrom.threadId
|
||||
: thread.lineage.parentThreadId;
|
||||
if (parentThreadId === null) continue;
|
||||
addEdge({
|
||||
sourceThreadId: parentThreadId,
|
||||
targetThreadId: thread.id,
|
||||
kind: thread.lineage.relationshipToParent === "subagent" ? "subagent" : "fork",
|
||||
status: thread.status,
|
||||
});
|
||||
}
|
||||
|
||||
if (input.projection !== null) {
|
||||
const ownerThreadId = input.projection.thread.id;
|
||||
for (const subagent of input.projection.subagents) {
|
||||
if (subagent.childThreadId === null) continue;
|
||||
addEdge({
|
||||
sourceThreadId: ownerThreadId,
|
||||
targetThreadId: subagent.childThreadId,
|
||||
kind: "subagent",
|
||||
status: subagent.status,
|
||||
});
|
||||
}
|
||||
for (const transfer of input.projection.contextTransfers) {
|
||||
if (transfer.sourceThreadId === transfer.targetThreadId) continue;
|
||||
addEdge({
|
||||
sourceThreadId: transfer.sourceThreadId,
|
||||
targetThreadId: transfer.targetThreadId,
|
||||
kind: "transfer",
|
||||
status: transfer.status,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { nodes, edges: [...edgesByKey.values()] };
|
||||
}
|
||||
|
||||
export function relatedThreadIds(
|
||||
graph: ThreadRelationshipGraph,
|
||||
threadId: ThreadId,
|
||||
): ReadonlyArray<ThreadId> {
|
||||
const ids = new Set<ThreadId>();
|
||||
for (const edge of graph.edges) {
|
||||
if (edge.sourceThreadId === threadId) ids.add(edge.targetThreadId);
|
||||
if (edge.targetThreadId === threadId) ids.add(edge.sourceThreadId);
|
||||
}
|
||||
return [...ids];
|
||||
}
|
||||
|
||||
export function walkThreadRelationships(
|
||||
graph: ThreadRelationshipGraph,
|
||||
threadId: ThreadId,
|
||||
): ReadonlyArray<ThreadRelationshipWalkRow> {
|
||||
const visited = new Set<ThreadId>([threadId]);
|
||||
const pending: Array<{ readonly threadId: ThreadId; readonly depth: number }> = [
|
||||
{ threadId, depth: 0 },
|
||||
];
|
||||
const rows: ThreadRelationshipWalkRow[] = [];
|
||||
|
||||
for (let index = 0; index < pending.length; index += 1) {
|
||||
const current = pending[index];
|
||||
if (current === undefined) continue;
|
||||
for (const edge of graph.edges) {
|
||||
const relatedId =
|
||||
edge.sourceThreadId === current.threadId
|
||||
? edge.targetThreadId
|
||||
: edge.targetThreadId === current.threadId
|
||||
? edge.sourceThreadId
|
||||
: null;
|
||||
if (relatedId === null || visited.has(relatedId)) continue;
|
||||
visited.add(relatedId);
|
||||
const depth = current.depth + 1;
|
||||
rows.push({ threadId: relatedId, fromThreadId: current.threadId, depth, edge });
|
||||
pending.push({ threadId: relatedId, depth });
|
||||
}
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
export function immediateThreadRelationships(
|
||||
graph: ThreadRelationshipGraph,
|
||||
threadId: ThreadId,
|
||||
): ReadonlyArray<ThreadRelationshipWalkRow> {
|
||||
const visited = new Set<ThreadId>();
|
||||
const rows: ThreadRelationshipWalkRow[] = [];
|
||||
|
||||
for (const edge of graph.edges) {
|
||||
const relatedId =
|
||||
edge.sourceThreadId === threadId
|
||||
? edge.targetThreadId
|
||||
: edge.targetThreadId === threadId
|
||||
? edge.sourceThreadId
|
||||
: null;
|
||||
if (relatedId === null || visited.has(relatedId)) continue;
|
||||
visited.add(relatedId);
|
||||
rows.push({ threadId: relatedId, fromThreadId: threadId, depth: 1, edge });
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import { ProviderModelPicker } from "../components/chat/ProviderModelPicker";
|
||||
import { usePrimarySettings } from "../hooks/useSettings";
|
||||
import { resolveMergeBackTargetThreadId } from "../lib/threadRelationships";
|
||||
import { resolveMergeBackTargetThreadId } from "@t3tools/client-runtime/state/thread-relationships";
|
||||
import { newCommandId, newMessageId, newProjectId, newThreadId } from "../lib/utils";
|
||||
import { type AppModelOption, getAppModelOptionsForInstance } from "../modelSelection";
|
||||
import { deriveOrchestrationV2DebugProviderSnapshots } from "../orchestrationV2DebugProviders";
|
||||
|
||||
Reference in new issue
Block a user