-
Notifications
You must be signed in to change notification settings - Fork 0
Chat | Allow new conversations while a stream is active #48
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Create Octavus agent sessions on a dedicated HTTP connection pool. | ||
| * | ||
| * Long-lived /api/trigger streams use the process-wide fetch dispatcher. When | ||
| * that pool is saturated (or pipelined behind a streaming response), a normal | ||
| * `octavus.agentSessions.create()` can stall until the stream ends — which | ||
| * blocks "New chat" in the UI. Routing creates through their own Agent keeps | ||
| * session creation independent of in-flight triggers. | ||
| */ | ||
| import { Agent, fetch as undiciFetch } from 'undici'; | ||
|
|
||
| const createAgent = new Agent({ | ||
| connections: 8, | ||
| pipelining: 0, | ||
| headersTimeout: 15_000, | ||
| bodyTimeout: 15_000, | ||
| }); | ||
|
|
||
| /** | ||
| * @param {object} opts | ||
| * @param {string} opts.baseUrl - Octavus API base URL. | ||
| * @param {string} [opts.apiKey] - Bearer token. | ||
| * @param {string} opts.agentId - Deployed agent id. | ||
| * @param {Record<string, unknown>} [opts.input] - Session input interpolations. | ||
| * @returns {Promise<string>} The new session id. | ||
| */ | ||
| export async function createAgentSession({ baseUrl, apiKey, agentId, input = {} }) { | ||
| if (!baseUrl) throw new Error('OCTAVUS_API_URL is not configured'); | ||
| if (!agentId) throw new Error('Octavus agent id is not configured'); | ||
|
|
||
| const url = `${String(baseUrl).replace(/\/$/, '')}/api/agent-sessions`; | ||
| const headers = { 'Content-Type': 'application/json' }; | ||
| if (apiKey) headers.Authorization = `Bearer ${apiKey}`; | ||
|
|
||
| const res = await undiciFetch(url, { | ||
| method: 'POST', | ||
| headers, | ||
| body: JSON.stringify({ agentId, input }), | ||
| dispatcher: createAgent, | ||
| }); | ||
|
|
||
| if (!res.ok) { | ||
| const text = await res.text().catch(() => ''); | ||
| throw new Error( | ||
| `Failed to create agent session (${res.status})${text ? `: ${text}` : ''}`, | ||
| ); | ||
| } | ||
|
|
||
| const data = await res.json(); | ||
| if (typeof data?.sessionId !== 'string' || !data.sessionId) { | ||
| throw new Error('Agent session create returned no sessionId'); | ||
| } | ||
| return data.sessionId; | ||
| } |
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,21 @@ | ||
| /** | ||
| * Serialize read-modify-write access to chat-sessions.json. | ||
| * | ||
| * Stream saves and session creates can overlap; without a queue, two handlers | ||
| * can read the same snapshot and the later write drops the earlier change. | ||
| */ | ||
|
|
||
| /** | ||
| * @param {() => Promise<unknown>} operation | ||
| * @param {{ chain?: Promise<unknown> }} [state] - Mutable holder for the queue tip. | ||
| * @returns {Promise<unknown>} | ||
| */ | ||
| export function enqueueSessionsWrite(operation, state = enqueueSessionsWrite) { | ||
| const prev = state.chain ?? Promise.resolve(); | ||
| const run = prev.then(operation, operation); | ||
| // Keep the chain alive after failures so later writes still run. | ||
| state.chain = run.catch(() => {}); | ||
| return run; | ||
| } | ||
|
|
||
| enqueueSessionsWrite.chain = Promise.resolve(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
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.