-
Notifications
You must be signed in to change notification settings - Fork 0
Thread previous_response_id through Tzafon and OpenAI CUA providers #46
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a09ae5
Thread Tzafon requests with previous_response_id + delta input
rgarcia 8ea308e
Add cua openai-cua-responses stream provider with previous_response_i…
rgarcia a37e84e
Anchor response threading on the latest assistant turn; fix stale sto…
rgarcia fad9354
Clear responseId on OpenAI error turns; route concrete Model inputs t…
rgarcia 15a2bda
Update cli test fixtures to the routed openai-cua-responses api
rgarcia 3ba365e
Reuse pi-ai's builtin OpenAI Responses stream; thread via onPayload
rgarcia 0f93fd4
Ignore responseId from errored/aborted turns in response threading
rgarcia c40fbe9
Note why the model needs `as never` when reusing pi-ai's builtin
rgarcia 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
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
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
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,53 @@ | ||
| import { | ||
| streamOpenAIResponses as piStreamOpenAIResponses, | ||
| streamSimpleOpenAIResponses as piStreamSimpleOpenAIResponses, | ||
| type Context, | ||
| type OpenAIResponsesOptions as PiOpenAIResponsesOptions, | ||
| type SimpleStreamOptions, | ||
| type StreamFunction, | ||
| type StreamOptions, | ||
| } from "@earendil-works/pi-ai"; | ||
| import { responseThreadingDelta, responseThreadingEnabled, type ResponseThreadingOptions } from "../common"; | ||
|
|
||
| export const OPENAI_CUA_RESPONSES_API = "openai-cua-responses"; | ||
|
|
||
| /** Stream options for the cua OpenAI Responses provider: pi-ai's options plus threading control. */ | ||
| export interface OpenAIResponsesOptions extends PiOpenAIResponsesOptions, ResponseThreadingOptions {} | ||
|
|
||
| type OnPayload = NonNullable<StreamOptions["onPayload"]>; | ||
|
|
||
| /** | ||
| * Prepare a request for pi-ai's builtin OpenAI Responses stream so it threads | ||
| * `previous_response_id`. The public Responses API requires `store: true` to | ||
| * chain, so the payload always stores; when a prior assistant `responseId` | ||
| * exists, only the delta messages are sent with `previous_response_id` set. | ||
| * Any caller `onPayload` runs on top of the threaded payload. | ||
| */ | ||
| export function threadRequest( | ||
| context: Context, | ||
| options: (ResponseThreadingOptions & { onPayload?: OnPayload }) | undefined, | ||
| ): { context: Context; onPayload: OnPayload } { | ||
| const delta = responseThreadingEnabled(options) ? responseThreadingDelta(context.messages) : undefined; | ||
| const previousResponseId = delta?.previousResponseId; | ||
| const messages = previousResponseId && delta ? delta.deltaMessages : context.messages; | ||
| const onPayload: OnPayload = async (payload, model) => { | ||
| const threaded = { | ||
| ...(payload as Record<string, unknown>), | ||
| store: true, | ||
| ...(previousResponseId ? { previous_response_id: previousResponseId } : {}), | ||
| }; | ||
| return options?.onPayload ? ((await options.onPayload(threaded, model)) ?? threaded) : threaded; | ||
| }; | ||
| return { context: messages === context.messages ? context : { ...context, messages }, onPayload }; | ||
| } | ||
|
|
||
| // pi-ai's builtin stream fns are typed to the "openai-responses" api; we reuse them under our routed api, hence `as never` on the model. | ||
| export const streamOpenAIResponses: StreamFunction<typeof OPENAI_CUA_RESPONSES_API, OpenAIResponsesOptions> = (model, context, options) => { | ||
| const threaded = threadRequest(context, options); | ||
| return piStreamOpenAIResponses(model as never, threaded.context, { ...options, onPayload: threaded.onPayload }); | ||
| }; | ||
|
|
||
| export const streamSimpleOpenAIResponses: StreamFunction<typeof OPENAI_CUA_RESPONSES_API, SimpleStreamOptions> = (model, context, options) => { | ||
| const threaded = threadRequest(context, options); | ||
| return piStreamSimpleOpenAIResponses(model as never, threaded.context, { ...options, onPayload: threaded.onPayload }); | ||
| }; |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.