Skip to content
Merged
2 changes: 1 addition & 1 deletion apps/server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const serverAt = (home: string, isDefault: boolean) =>
const info = yield* Codework.call((client) => client("server.info", {})).pipe(Effect.option);
if (Option.isNone(info) || info.value.pid !== pid) return { _tag: "Unreachable", url, pid } satisfies CliServer;
const { version } = info.value;
const usable = yield* Codework.call((client) => client("session.list", {})).pipe(Effect.isSuccess);
const usable = yield* Codework.call((client) => client("thread.list", {})).pipe(Effect.isSuccess);
return { _tag: usable ? "Running" : "Incompatible", url, pid, version } satisfies CliServer;
});

Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/codework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import path from "node:path";
import { CodeworkUnavailable } from "@codeworksh/contract";
import { Servers } from "./servers.ts";

export type SessionInfo = Mirror.SessionInfo;
export type ThreadInfo = Mirror.ThreadInfo;

/** What an untitled thread is called until its first prompt names it. */
export const UNTITLED = Mirror.UNTITLED;

/** How a running server advertises itself: `<home>/service.json`, written by `codework serve`. */
const Registration = Schema.fromJsonString(
Expand Down
18 changes: 10 additions & 8 deletions apps/server/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import { Servers } from "./servers.ts";
import { Workspaces } from "./workspaces.ts";

/**
* Sessions grouped by project, both newest first. A session whose project row is missing gets a
* group of its own folder, so it still shows rather than vanishing.
* Sessions grouped by project, both newest first, each shown as its main thread. A session whose
* project row is missing gets a group of its own folder, so it still shows rather than vanishing.
*/
const byProject = (sessions: ReadonlyArray<Codework.SessionInfo>): Project[] => {
const byProject = (threads: ReadonlyArray<Codework.ThreadInfo>): Project[] => {
const groups = new Map<string, Project & { sessions: Project["sessions"][number][] }>();
for (const session of [...sessions].sort((a, b) => b.updatedAt - a.updatedAt)) {
const mains = threads.filter((thread) => thread.origin.kind === "main");
for (const thread of [...mains].sort((a, b) => b.updatedAt - a.updatedAt)) {
const session = thread.session;
const folder = session.hostDir ?? session.directory;
const key = session.project?.id ?? folder;
const group = groups.get(key) ?? {
Expand All @@ -34,9 +36,9 @@ const byProject = (sessions: ReadonlyArray<Codework.SessionInfo>): Project[] =>
};
group.sessions.push({
id: session.id,
title: session.title,
updated: session.updatedAt,
...(session.model === undefined ? {} : { model: session.model }),
title: thread.title ?? Codework.UNTITLED,
updated: thread.updatedAt,
...(thread.model === undefined ? {} : { model: thread.model }),
});
groups.set(key, group);
}
Expand Down Expand Up @@ -67,7 +69,7 @@ export const layer = Contract.Api.toLayer(
...desk,

"projects.list": Effect.fn("projects.list")(function* () {
return byProject(yield* call((client) => client("session.list", {})));
return byProject(yield* call((client) => client("thread.list", {})));
}),

// The catalog's own errors are kept apart from an unreachable server, so their detail shows.
Expand Down
32 changes: 18 additions & 14 deletions apps/web/src/widgets/session/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
HistoryEntry,
type Part,
QueuedPrompt,
type SessionInfo,
type ThreadInfo,
UNTITLED,
} from "@codeworksh/widget/codework";
import { Deferred, Effect, Fiber, Option, Schedule, Schema, Stream } from "effect";

Expand All @@ -29,13 +30,14 @@ import {
type Window,
} from "./schema.ts";

const toInfo = (info: SessionInfo): Info => ({
title: info.title,
// A session shows as its main thread until threads get a view of their own.
const toInfo = (info: ThreadInfo): Info => ({
title: info.title ?? UNTITLED,
updated: info.updatedAt,
...(info.hostDir === undefined ? {} : { hostDir: info.hostDir }),
...(info.sandbox === undefined ? {} : { sandbox: info.sandbox.driver }),
...(info.session.hostDir === undefined ? {} : { hostDir: info.session.hostDir }),
...(info.session.sandbox === undefined ? {} : { sandbox: info.session.sandbox.driver }),
...(info.thinkingLevel === undefined ? {} : { thinkingLevel: info.thinkingLevel }),
...(info.project === undefined ? {} : { project: info.project }),
...(info.session.project === undefined ? {} : { project: info.session.project }),
...(info.model === undefined ? {} : { model: info.model }),
});

Expand Down Expand Up @@ -180,9 +182,9 @@ export const session = defineBackend({
const refresh = Effect.fn("Session.refresh")(function* (id: string, cursor: number | undefined) {
// In this order: a prompt promoted between the queue and history reads is then in both, never in neither.
const { info, queue, history } = yield* Effect.all({
info: codework("session.info", { sessionId: id }),
queue: codework("session.queue", { sessionId: id }),
history: codework("session.history", { sessionId: id }),
info: codework("thread.info", { sessionId: id }),
queue: codework("thread.queue", { sessionId: id }),
history: codework("thread.history", { sessionId: id }),
});
const before = yield* load(id);
const next = toInfo(info);
Expand Down Expand Up @@ -410,7 +412,7 @@ export const session = defineBackend({
);

const prompt = ({ sessionId, messageId, text, delivery }: Prompt) =>
codework("session.prompt", {
codework("thread.prompt", {
sessionId,
text,
id: messageId,
Expand All @@ -437,7 +439,9 @@ export const session = defineBackend({
...(thinkingLevel === undefined ? {} : { thinkingLevel }),
},
}).pipe(
Effect.flatMap((info) => prompt({ sessionId: info.id, messageId, text }).pipe(Effect.as(info.id))),
Effect.flatMap((info) =>
prompt({ sessionId: info.session.id, messageId, text }).pipe(Effect.as(info.session.id)),
),
Effect.map((sessionId): Created => ({ _tag: "Created", sessionId })),
Effect.catch((error) => Effect.succeed<Created>({ _tag: "Failed", message: describe(error) })),
Effect.flatMap((created) => encodeCreated(created).pipe(Effect.orDie)),
Expand All @@ -446,7 +450,7 @@ export const session = defineBackend({

// The session's own log moves with the change, so the refresh that follows publishes the new info.
yield* handle(topics.configure, Configure, ({ sessionId, model, thinkingLevel }) =>
codework("session.configure", {
codework("thread.configure", {
sessionId,
runtime: {
...(model === undefined ? {} : { model }),
Expand All @@ -462,15 +466,15 @@ export const session = defineBackend({
// A cancel that lost to promotion answers `Sent`: the prompt is in the conversation, and the
// refresh that follows the log moving shows it there.
yield* handle(topics.cancel, Cancel, ({ sessionId, messageId }) =>
codework("session.cancel", { sessionId, messageId }).pipe(
codework("thread.cancel", { sessionId, messageId }).pipe(
Effect.map(({ cancelled }): Cancelled => (cancelled ? { _tag: "Cancelled" } : { _tag: "Sent" })),
Effect.catch((error) => Effect.succeed<Cancelled>({ _tag: "Failed", message: describe(error) })),
Effect.flatMap((reply) => encodeCancelled(reply).pipe(Effect.orDie)),
),
);

yield* handle(topics.change, Change, ({ sessionId, messageId, delivery, text }) =>
codework("session.update", {
codework("thread.update", {
sessionId,
messageId,
...(delivery === undefined ? {} : { delivery }),
Expand Down
103 changes: 66 additions & 37 deletions apps/widget/src/codework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type ToolCall = typeof ToolCall.Type;
export const Part = Schema.Union([TextContent, ImageContent, ThinkingContent, ToolCall]);
export type Part = typeof Part.Type;

/** One message on a session's active branch, oldest first. */
/** One message in a thread's line, inherited ones first, oldest first. */
export const HistoryEntry = Schema.Struct({
id: Schema.String,
seq: Schema.Int,
Expand All @@ -65,18 +65,33 @@ export type ThinkingLevel = typeof ThinkingLevel.Type;

export const SessionInfo = Schema.Struct({
id: Schema.String,
title: Schema.String,
directory: Schema.String,
hostDir: Schema.optionalKey(Schema.String),
/** The sandbox the session runs in; absent for the host itself. */
sandbox: Schema.optionalKey(Schema.Struct({ driver: Schema.String })),
project: Schema.optionalKey(Schema.Struct({ id: Schema.String, name: Schema.String })),
updatedAt: Schema.Finite,
/** Where unaddressed prompts go. */
mainThreadId: Schema.String,
});
export type SessionInfo = typeof SessionInfo.Type;

/** One linear transcript of a session, with the session embedded. */
export const ThreadInfo = Schema.Struct({
id: Schema.String,
origin: Schema.Struct({ kind: Schema.Literals(["main", "fork", "subagent"]) }),
/** Absent until its first prompt. */
title: Schema.optionalKey(Schema.String),
updatedAt: Schema.Finite,
model: Schema.optionalKey(Schema.Struct({ provider: Schema.String, id: Schema.String })),
/** The thinking level chosen for this session; absent when settings decide. */
/** The thinking level chosen for this thread; absent when settings decide. */
thinkingLevel: Schema.optionalKey(ThinkingLevel),
session: SessionInfo,
});
export type SessionInfo = typeof SessionInfo.Type;
export type ThreadInfo = typeof ThreadInfo.Type;

/** What a client calls a thread its first prompt has not named yet. */
export const UNTITLED = "Session";

/** A harness event as the server sends it; `data` stays encoded, decoded per `type` by its reader. */
export const EventEnvelope = Schema.Struct({
Expand Down Expand Up @@ -116,9 +131,9 @@ export class SessionNotFoundError extends Schema.TaggedError<SessionNotFoundErro
sessionId: Schema.String,
}) {}

/** A prompt reused a message id the session already holds for another prompt. */
/** A prompt reused a message id the thread already holds for another prompt. */
export class PromptConflictError extends Schema.TaggedError<PromptConflictError>()("PromptConflictError", {
sessionId: Schema.String,
threadId: Schema.String,
messageId: Schema.String,
}) {}

Expand Down Expand Up @@ -194,6 +209,7 @@ export type RuntimeConfig = typeof RuntimeConfig.Type;
*/
export const CreateError = Schema.Union([
SessionNotFoundError,
Schema.TaggedStruct("ThreadNotFoundError", { threadId: Schema.String }),
Schema.TaggedStruct("SessionLinkedSpaceNotFoundError", { sessionId: Schema.String }),
Schema.TaggedStruct("RelinkError", { sessionId: Schema.String, reason: Schema.String }),
Schema.TaggedStruct("SandboxNotFoundError", { id: Schema.String }),
Expand All @@ -210,6 +226,16 @@ export const CreateError = Schema.Union([
]);
export type CreateError = typeof CreateError.Type;

/** A thread of a session; the session's main thread when `threadId` is absent. */
const ThreadRef = { sessionId: Schema.String, threadId: Schema.optionalKey(Schema.String) };

/** The server's "no such thread" (the harness `ThreadNotFoundError`), by its wire tag. */
export class ThreadNotFoundError extends Schema.TaggedError<ThreadNotFoundError>()("ThreadNotFoundError", {
threadId: Schema.String,
}) {}

const ThreadError = Schema.Union([SessionNotFoundError, ThreadNotFoundError]);

export const Api = RpcGroup.make(
Rpc.make("server.info", {
payload: {},
Expand All @@ -229,7 +255,8 @@ export const Api = RpcGroup.make(
/** Names the project if this creates it; an existing project keeps its name. Trimmed, non-empty. */
project: Schema.optionalKey(Schema.Struct({ name: Schema.String })),
},
success: SessionInfo,
/** The session's main thread. */
success: ThreadInfo,
error: CreateError,
}),
/** The models a session anchored at `hostDir` can use; without it, only the user's settings apply. */
Expand All @@ -238,33 +265,34 @@ export const Api = RpcGroup.make(
success: ModelList,
error: Schema.Union([SettingsError, ModelCatalogError]),
}),
Rpc.make("session.list", {
payload: {},
success: Schema.Array(SessionInfo),
/** Threads newest first, of one session or all. */
Rpc.make("thread.list", {
payload: { sessionId: Schema.optionalKey(Schema.String) },
success: Schema.Array(ThreadInfo),
}),
/** Sets the session's model and thinking level over its settings; they apply from its next turn. */
Rpc.make("session.configure", {
payload: { sessionId: Schema.String, runtime: RuntimeConfig },
success: SessionInfo,
error: SessionNotFoundError,
/** Sets the thread's model and thinking level over its settings; they apply from its next request. */
Rpc.make("thread.configure", {
payload: { ...ThreadRef, runtime: RuntimeConfig },
success: ThreadInfo,
error: ThreadError,
}),
Rpc.make("session.info", {
payload: { sessionId: Schema.String },
success: SessionInfo,
error: SessionNotFoundError,
Rpc.make("thread.info", {
payload: ThreadRef,
success: ThreadInfo,
error: ThreadError,
}),
Rpc.make("session.history", {
payload: { sessionId: Schema.String },
Rpc.make("thread.history", {
payload: ThreadRef,
success: Schema.Struct({ entries: Schema.Array(HistoryEntry) }),
error: SessionNotFoundError,
error: ThreadError,
}),
/** Prompts admitted but not yet promoted, in admission order; reread whenever the log moves. */
Rpc.make("session.queue", {
payload: { sessionId: Schema.String },
Rpc.make("thread.queue", {
payload: ThreadRef,
success: Schema.Struct({ entries: Schema.Array(QueuedPrompt) }),
error: SessionNotFoundError,
error: ThreadError,
}),
/** Durable events after `after` (exclusive), then `log.synced`; with `follow`, each new one as it commits. */
/** Durable events of every thread after `after` (exclusive), then `log.synced`; with `follow`, each new one as it commits. */
Rpc.make("session.log", {
payload: {
sessionId: Schema.String,
Expand All @@ -276,33 +304,34 @@ export const Api = RpcGroup.make(
stream: true,
}),
/** `id` names the user message the prompt becomes; it is the prompt's correlation id. */
Rpc.make("session.prompt", {
Rpc.make("thread.prompt", {
payload: {
sessionId: Schema.String,
...ThreadRef,
text: Schema.String,
delivery: Schema.optionalKey(Delivery),
id: Schema.optionalKey(Schema.String),
},
success: Schema.Void,
error: Schema.Union([SessionNotFoundError, PromptConflictError]),
success: Schema.Struct({ threadId: Schema.String }),
error: Schema.Union([ThreadError, PromptConflictError]),
}),
/** Withdraws a queued prompt; `cancelled` is false when it was already promoted or is unknown. */
Rpc.make("session.cancel", {
payload: { sessionId: Schema.String, messageId: Schema.String },
Rpc.make("thread.cancel", {
payload: { ...ThreadRef, messageId: Schema.String },
success: Schema.Struct({ cancelled: Schema.Boolean }),
error: SessionNotFoundError,
error: ThreadError,
}),
/** Moves a queued prompt to the other lane or rewrites it in place; `delivery: "steer"` also wakes the session. */
Rpc.make("session.update", {
/** Moves a queued prompt to the other lane or rewrites it in place; `delivery: "steer"` also wakes the thread. */
Rpc.make("thread.update", {
payload: {
sessionId: Schema.String,
...ThreadRef,
messageId: Schema.String,
delivery: Schema.optionalKey(Delivery),
text: Schema.optionalKey(Schema.String),
},
success: Schema.Struct({ updated: Schema.Boolean }),
error: SessionNotFoundError,
error: ThreadError,
}),
/** Stops every thread of the session. */
Rpc.make("session.interrupt", {
payload: { sessionId: Schema.String },
success: Schema.Struct({ interrupted: Schema.Boolean }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,37 @@
"providerThinkingLevel": "max"
}
},
"claude-haiku-5-5": {
"off": {
"thinkingLevel": "off",
"providerThinkingLevel": null,
"thinking": false
},
"minimal": {
"thinkingLevel": "minimal",
"providerThinkingLevel": "low"
},
"low": {
"thinkingLevel": "low",
"providerThinkingLevel": "low"
},
"medium": {
"thinkingLevel": "medium",
"providerThinkingLevel": "medium"
},
"high": {
"thinkingLevel": "high",
"providerThinkingLevel": "high"
},
"xhigh": {
"thinkingLevel": "xhigh",
"providerThinkingLevel": "xhigh"
},
"max": {
"thinkingLevel": "max",
"providerThinkingLevel": "max"
}
},
"claude-haiku-4-5-20251001": {
"off": {
"thinkingLevel": "off",
Expand Down
4 changes: 2 additions & 2 deletions packages/codework/src/acp/feed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as Acp from "@agentclientprotocol/sdk";
import { EventList, type EventSchema, type SessionStore } from "@codeworksh/harness/effect";
import { EventList, type EventSchema, type ThreadStore } from "@codeworksh/harness/effect";
import { Option, Schema } from "effect";

const isTextDelta = Schema.is(EventList.LLMTextDelta);
Expand Down Expand Up @@ -92,7 +92,7 @@ export const noticeUpdate = (notice: string): Acp.SessionUpdate => ({
});

/** The `session/update`s that replay one stored entry during `session/load`. */
export const replay = ({ entry, parts }: SessionStore.HydratedEntry): ReadonlyArray<Acp.SessionUpdate> => {
export const replay = ({ entry, parts }: ThreadStore.HydratedEntry): ReadonlyArray<Acp.SessionUpdate> => {
if (entry.type !== "user" && entry.type !== "assistant") return [];
return parts.flatMap((row): ReadonlyArray<Acp.SessionUpdate> => {
const part = decodePart(row.data);
Expand Down
Loading
Loading