Repository navigation
Add hosted child fork run context helper #1467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { assertEquals } from "@std/assert"; | ||
| import type { ChatMessageMetadata, ChatUiMessageChunk } from "#veryfront/chat/protocol.ts"; | ||
| import { createHostedChildForkRunContext } from "./hosted-child-fork-run-context.ts"; | ||
|
|
||
| Deno.test("createHostedChildForkRunContext wires stream mirror state and buffers", async () => { | ||
| const chunks: ChatUiMessageChunk<ChatMessageMetadata>[] = []; | ||
| const context = createHostedChildForkRunContext({ | ||
| mirror: { | ||
| handleChunk: (chunk) => { | ||
| chunks.push(chunk); | ||
| }, | ||
| }, | ||
| messageId: "message-1", | ||
| pendingToolLogContext: { | ||
| conversationId: "conversation-1", | ||
| parentRunId: "run-1", | ||
| description: "Check the app", | ||
| }, | ||
| }); | ||
|
|
||
| assertEquals(context.streamMirrorContext.durableRunMirror, true); | ||
| assertEquals(context.streamMirrorContext.durableMessageId, "message-1"); | ||
| assertEquals(context.streamMirrorContext.durableReasoningMessageId, "message-1:reasoning"); | ||
| assertEquals(context.streamMirrorContext.hasStartedStep(), false); | ||
| assertEquals(context.streamMirrorContext.hasEmittedProgress(), false); | ||
| assertEquals(context.streamState.finalText, ""); | ||
| assertEquals(context.toolCalls, []); | ||
| assertEquals(context.toolResults, []); | ||
|
|
||
| context.streamMirrorContext.markDurableStepStarted(); | ||
| await context.streamMirrorContext.appendDurableMirrorChunk({ type: "start-step" }); | ||
|
|
||
| assertEquals(context.streamMirrorContext.hasStartedStep(), true); | ||
| assertEquals(context.streamMirrorContext.hasEmittedProgress(), true); | ||
| assertEquals(chunks, [{ type: "start-step" }]); | ||
| }); | ||
|
|
||
| Deno.test("createHostedChildForkRunContext closes pending tool calls with host logger", async () => { | ||
| const chunks: ChatUiMessageChunk<ChatMessageMetadata>[] = []; | ||
| const warnings: Array<{ message: string; context: Record<string, unknown> }> = []; | ||
| const context = createHostedChildForkRunContext({ | ||
| mirror: { | ||
| handleChunk: (chunk) => { | ||
| chunks.push(chunk); | ||
| }, | ||
| }, | ||
| pendingToolLogContext: { | ||
| conversationId: "conversation-1", | ||
| parentRunId: "run-1", | ||
| description: "Check the app", | ||
| }, | ||
| pendingToolLogWriter: { | ||
| warn: (message, logContext) => { | ||
| warnings.push({ message, context: logContext }); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| context.pendingToolLifecycle.upsertPendingToolCall("tool-call-1", { | ||
| phase: "awaiting_result", | ||
| toolName: "read_file", | ||
| input: { path: "README.md" }, | ||
| }); | ||
|
|
||
| await context.pendingToolLifecycle.closePendingToolCalls({ kind: "aborted" }); | ||
|
|
||
| assertEquals(chunks.map((chunk) => chunk.type), ["tool-input-start", "tool-output-error"]); | ||
| assertEquals(warnings.length, 1); | ||
| assertEquals(warnings[0]?.message, "Closing incomplete child fork tool lifecycles"); | ||
| assertEquals(warnings[0]?.context, { | ||
| conversationId: "conversation-1", | ||
| runId: "run-1", | ||
| description: "Check the app", | ||
| reason: "aborted", | ||
| toolCallIds: ["tool-call-1"], | ||
| errorMessage: null, | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import type { ChatMessageMetadata, ChatUiMessageChunk } from "#veryfront/chat/protocol.ts"; | ||
| import { | ||
| createHostedChildMirrorContext, | ||
| type HostedChildChunkMirror, | ||
| type HostedChildMirrorContext, | ||
| } from "./hosted-child-mirror.ts"; | ||
| import { | ||
| createHostedChildPendingToolLifecycle, | ||
| createHostedChildPendingToolLifecycleLogger, | ||
| type HostedChildPendingToolLifecycleLogContext, | ||
| type HostedChildPendingToolLifecycleLogWriter, | ||
| } from "./hosted-child-pending-tool-lifecycle.ts"; | ||
| import type { HostedChildForkPendingToolLifecycle } from "./hosted-child-fork-stream-execution.ts"; | ||
|
|
||
| export interface HostedChildForkToolCallSnapshot { | ||
| toolName: string; | ||
| toolCallId: string; | ||
| input?: unknown; | ||
| } | ||
|
|
||
| export interface HostedChildForkToolResultSnapshot { | ||
| toolName: string; | ||
| toolCallId: string; | ||
| input: unknown; | ||
| output: unknown; | ||
| } | ||
|
|
||
| export interface HostedChildForkStreamState { | ||
| finalText: string; | ||
| } | ||
|
|
||
| export interface HostedChildForkStreamMirrorContext { | ||
| durableRunMirror: boolean; | ||
| durableMessageId: string | null; | ||
| durableReasoningMessageId: string | null; | ||
| durableMirrorState: HostedChildMirrorContext["state"]; | ||
| appendDurableMirrorChunk: (chunk: ChatUiMessageChunk<ChatMessageMetadata>) => Promise<void>; | ||
| closeDurableMirrorReasoning: () => Promise<void>; | ||
| closeDurableMirrorText: () => Promise<void>; | ||
| markDurableStepStarted: () => void; | ||
| hasStartedStep: () => boolean; | ||
| hasEmittedProgress: () => boolean; | ||
| } | ||
|
|
||
| export interface HostedChildForkRunContext { | ||
| mirrorContext: HostedChildMirrorContext; | ||
| streamMirrorContext: HostedChildForkStreamMirrorContext; | ||
| pendingToolLifecycle: HostedChildForkPendingToolLifecycle; | ||
| toolCalls: HostedChildForkToolCallSnapshot[]; | ||
| toolResults: HostedChildForkToolResultSnapshot[]; | ||
| streamState: HostedChildForkStreamState; | ||
| } | ||
|
|
||
| export interface HostedChildForkRunContextInput { | ||
| mirror: HostedChildChunkMirror | null; | ||
| messageId?: string | null; | ||
| reasoningMessageId?: string | null; | ||
| pendingToolLogContext: HostedChildPendingToolLifecycleLogContext; | ||
| pendingToolLogWriter?: HostedChildPendingToolLifecycleLogWriter; | ||
| } | ||
|
|
||
| export function createHostedChildForkRunContext( | ||
| input: HostedChildForkRunContextInput, | ||
| ): HostedChildForkRunContext { | ||
| const mirrorContext = createHostedChildMirrorContext({ | ||
| mirror: input.mirror, | ||
| messageId: input.messageId, | ||
| reasoningMessageId: input.reasoningMessageId, | ||
| }); | ||
| const streamMirrorContext: HostedChildForkStreamMirrorContext = { | ||
| durableRunMirror: Boolean(mirrorContext.mirror), | ||
| durableMessageId: mirrorContext.messageId, | ||
| durableReasoningMessageId: mirrorContext.reasoningMessageId, | ||
| durableMirrorState: mirrorContext.state, | ||
| appendDurableMirrorChunk: mirrorContext.appendChunk, | ||
| closeDurableMirrorReasoning: mirrorContext.closeReasoningSegment, | ||
| closeDurableMirrorText: mirrorContext.closeTextSegment, | ||
| markDurableStepStarted: mirrorContext.markStepStarted, | ||
| hasStartedStep: mirrorContext.hasStartedStep, | ||
| hasEmittedProgress: mirrorContext.hasEmittedProgress, | ||
| }; | ||
|
|
||
| return { | ||
| mirrorContext, | ||
| streamMirrorContext, | ||
| pendingToolLifecycle: createHostedChildPendingToolLifecycle({ | ||
| appendMirrorChunk: streamMirrorContext.appendDurableMirrorChunk, | ||
| logger: input.pendingToolLogWriter | ||
| ? createHostedChildPendingToolLifecycleLogger( | ||
| input.pendingToolLogContext, | ||
| input.pendingToolLogWriter, | ||
| ) | ||
| : undefined, | ||
| }), | ||
| toolCalls: [], | ||
| toolResults: [], | ||
| streamState: { finalText: "" }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| // Keep in sync with deno.json version. | ||
| // scripts/release.ts updates this constant during releases. | ||
| export const VERSION = "0.1.405"; | ||
| export const VERSION = "0.1.406"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this helper is used to feed
executeHostedChildForkStreamwith amirrorbut without the optionalmessageId, this setsdurableRunMirrorto true and the executor immediately mirrorsstart-step, makinghasEmittedProgress()true. Subsequent text chunks are not mirrored becausehandleHostedChildForkStreamPartonly emits text whendurableMessageIdis present, and final fallback chunks are also skipped because progress was already emitted, so the mirrored child run can end up with no assistant text. Either require/generate a message id for mirrored runs or do not enable the durable mirror without one.Useful? React with 👍 / 👎.