mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-10 11:40:45 +08:00
feat: support per-bot workspace switching
This commit is contained in:
parent
adcace3512
commit
44dd50216a
64 changed files with 3878 additions and 712 deletions
44
src/channels/shared/workspace-session.mjs
Normal file
44
src/channels/shared/workspace-session.mjs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
export const WORKSPACE_SESSION_STALE = 'workspace-session-stale';
|
||||
|
||||
async function sessionExists(harness, sessionId, options) {
|
||||
return options === undefined
|
||||
? harness.sessionExists(sessionId)
|
||||
: harness.sessionExists(sessionId, options);
|
||||
}
|
||||
|
||||
async function createSession(harness, options) {
|
||||
return options === undefined
|
||||
? harness.createSession()
|
||||
: harness.createSession(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve, persist, and ask through a session that belongs to the bot's
|
||||
* current workspace. A concurrent workspace switch invalidates the scoped
|
||||
* session and retries before any prompt is sent to the stale session.
|
||||
*/
|
||||
export async function askInWorkspaceSession({
|
||||
harness,
|
||||
state,
|
||||
key,
|
||||
text,
|
||||
createOptions,
|
||||
existsOptions,
|
||||
askOptions,
|
||||
}) {
|
||||
while (true) {
|
||||
let sessionId = state.sessionFor(key);
|
||||
if (!sessionId || !(await sessionExists(harness, sessionId, existsOptions))) {
|
||||
sessionId = await createSession(harness, createOptions);
|
||||
if (await state.setSession(key, sessionId) === false) continue;
|
||||
}
|
||||
try {
|
||||
return {
|
||||
sessionId,
|
||||
answer: await harness.ask(sessionId, text, askOptions),
|
||||
};
|
||||
} catch (error) {
|
||||
if (error?.code !== WORKSPACE_SESSION_STALE) throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue