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
9 changes: 7 additions & 2 deletions apps/web/src/widgets/session/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,14 @@ const TurnView = memo(function TurnView({ turn, active }: { readonly turn: Turn;
.flatMap((reply) => reply.parts.filter((part) => !(reply === last && part.type === "text")))
.filter(isWork);
const interrupted = last?.state === "aborted";
const failed = last?.error !== undefined;
// While the agent works and no answer is streaming, it is thinking or running a tool.
const thinking = active && (last?.state !== "draft" || answer.length === 0);

return (
<section className={`${contentWidth} flex flex-col gap-5 px-4 py-3 text-body`}>
{turn.user !== undefined && <UserMessage entry={turn.user} />}
{(work.length > 0 || thinking || answer.length > 0 || interrupted) && (
{(work.length > 0 || thinking || answer.length > 0 || interrupted || failed) && (
<Message role="assistant" at={turn.replies[0]?.createdAt}>
{work.length > 0 && (
<WorkLog
Expand All @@ -408,7 +409,11 @@ const TurnView = memo(function TurnView({ turn, active }: { readonly turn: Turn;
<Markdown text={answer.join("\n\n")} streaming={last?.state === "draft"} />
</div>
)}
{interrupted && <p className="text-xs text-ink-muted">Interrupted</p>}
{last?.error !== undefined ? (
<p className="text-xs text-destructive">{last.error}</p>
) : (
interrupted && <p className="text-xs text-ink-muted">Interrupted</p>
)}
</Message>
)}
</section>
Expand Down
2 changes: 2 additions & 0 deletions apps/widget/src/codework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const HistoryEntry = Schema.Struct({
/** Epoch milliseconds of the last change; for a reply, when it ended once it is no longer a draft. */
updatedAt: Schema.Finite,
parts: Schema.Array(Part),
/** Why a reply failed, as `provider/model: reason`; absent for one that ended normally or was stopped. */
error: Schema.optionalKey(Schema.String),
});
export type HistoryEntry = typeof HistoryEntry.Type;

Expand Down
2 changes: 2 additions & 0 deletions packages/codework/src/server/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export const HistoryEntry = Schema.Struct({
/** Epoch milliseconds of the last change; for a reply, when it ended once it is no longer a draft. */
updatedAt: Schema.Finite,
parts: Schema.Array(Part),
/** Why a reply failed, as `provider/model: reason`; absent for one that ended normally or was stopped. */
error: optional(Schema.String),
});
export type HistoryEntry = typeof HistoryEntry.Type;

Expand Down
20 changes: 19 additions & 1 deletion packages/codework/src/server/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ThreadSchema,
ThreadStore,
} from "@codeworksh/harness/effect";
import { DateTime, Effect, Option, Stream } from "effect";
import { DateTime, Effect, Option, Schema, Stream } from "effect";
import {
Contract,
type HistoryEntry,
Expand Down Expand Up @@ -77,6 +77,20 @@ const runtimeInput = (runtime: RuntimeConfig | undefined): Session.RuntimeInput
...(runtime?.foundation == null ? {} : { foundation: runtime.foundation }),
});

// A failed reply's envelope keeps the provider's reason; it reads as the live failure does.
const decodeFailedReply = Schema.decodeUnknownOption(
Schema.fromJsonString(
Schema.Struct({
stopReason: Schema.Literal("error"),
errorMessage: Schema.String,
provider: Schema.Struct({ id: Schema.String }),
model: Schema.String,
}),
),
);
const replyError = (data: string) =>
Option.map(decodeFailedReply(data), (reply) => `${reply.provider.id}/${reply.model}: ${reply.errorMessage}`);

const threadInfo = (threadId: ThreadSchema.ID) => Session.threadInfo(threadId).pipe(Effect.map(toThreadInfo));

// RpcServer's request span has no level, so it is Debug and drops every Info span under it.
Expand Down Expand Up @@ -278,6 +292,10 @@ export const layer = Contract.Api.toLayer(
updatedAt: DateTime.toEpochMillis(entry.updatedAt),
// A part this schema does not render (a future aikit type) is skipped, not fatal.
parts: parts.flatMap((part) => Option.toArray(MessagePart.decode(part.data))),
...Option.match(entry.type === "assistant" ? replyError(entry.data) : Option.none(), {
onNone: () => ({}),
onSome: (error) => ({ error }),
}),
},
]
: [],
Expand Down
Loading