feat: added questions and permissions
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { create } from "zustand";
|
||||
import { devtools, persist, createJSONStorage } from "zustand/middleware";
|
||||
import { opencodeClient } from "@/lib/opencode/client";
|
||||
import type { QuestionRequest } from "@/types/question";
|
||||
import { getSafeStorage } from "./utils/safeStorage";
|
||||
import { useSessionStore } from "./sessionStore";
|
||||
|
||||
interface QuestionState {
|
||||
questions: Map<string, QuestionRequest[]>;
|
||||
}
|
||||
|
||||
interface QuestionActions {
|
||||
addQuestion: (question: QuestionRequest) => void;
|
||||
dismissQuestion: (sessionId: string, requestId: string) => void;
|
||||
respondToQuestion: (sessionId: string, requestId: string, answers: string[] | string[][]) => Promise<void>;
|
||||
rejectQuestion: (sessionId: string, requestId: string) => Promise<void>;
|
||||
}
|
||||
|
||||
type QuestionStore = QuestionState & QuestionActions;
|
||||
|
||||
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
|
||||
|
||||
const sanitizeQuestionEntries = (value: unknown): Array<[string, QuestionRequest[]]> => {
|
||||
if (!Array.isArray(value)) {
|
||||
return [];
|
||||
}
|
||||
const entries: Array<[string, QuestionRequest[]]> = [];
|
||||
value.forEach((entry) => {
|
||||
if (!Array.isArray(entry) || entry.length !== 2) {
|
||||
return;
|
||||
}
|
||||
const [sessionId, questions] = entry;
|
||||
if (typeof sessionId !== "string" || !Array.isArray(questions)) {
|
||||
return;
|
||||
}
|
||||
entries.push([sessionId, questions as QuestionRequest[]]);
|
||||
});
|
||||
return entries;
|
||||
};
|
||||
|
||||
const executeWithQuestionDirectory = async <T>(sessionId: string, operation: () => Promise<T>): Promise<T> => {
|
||||
try {
|
||||
const sessionStore = useSessionStore.getState();
|
||||
const directory = sessionStore.getDirectoryForSession(sessionId);
|
||||
if (directory) {
|
||||
return opencodeClient.withDirectory(directory, operation);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Failed to resolve session directory for question handling:", error);
|
||||
}
|
||||
return operation();
|
||||
};
|
||||
|
||||
export const useQuestionStore = create<QuestionStore>()(
|
||||
devtools(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
questions: new Map(),
|
||||
|
||||
addQuestion: (question: QuestionRequest) => {
|
||||
const sessionId = question.sessionID;
|
||||
if (!sessionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = get().questions.get(sessionId);
|
||||
if (existing?.some((entry) => entry.id === question.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
set((state) => {
|
||||
const sessionQuestions = state.questions.get(sessionId) || [];
|
||||
const next = new Map(state.questions);
|
||||
next.set(sessionId, [...sessionQuestions, question]);
|
||||
return { questions: next };
|
||||
});
|
||||
},
|
||||
|
||||
dismissQuestion: (sessionId: string, requestId: string) => {
|
||||
if (!sessionId || !requestId) {
|
||||
return;
|
||||
}
|
||||
|
||||
set((state) => {
|
||||
const sessionQuestions = state.questions.get(sessionId) || [];
|
||||
const updated = sessionQuestions.filter((q) => q.id !== requestId);
|
||||
const next = new Map(state.questions);
|
||||
next.set(sessionId, updated);
|
||||
return { questions: next };
|
||||
});
|
||||
},
|
||||
|
||||
respondToQuestion: async (sessionId: string, requestId: string, answers: string[] | string[][]) => {
|
||||
await executeWithQuestionDirectory(sessionId, () => opencodeClient.replyToQuestion(requestId, answers));
|
||||
get().dismissQuestion(sessionId, requestId);
|
||||
},
|
||||
|
||||
rejectQuestion: async (sessionId: string, requestId: string) => {
|
||||
await executeWithQuestionDirectory(sessionId, () => opencodeClient.rejectQuestion(requestId));
|
||||
get().dismissQuestion(sessionId, requestId);
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "question-store",
|
||||
storage: createJSONStorage(() => getSafeStorage()),
|
||||
partialize: (state) => ({
|
||||
questions: Array.from(state.questions.entries()),
|
||||
}),
|
||||
merge: (persistedState, currentState) => {
|
||||
if (!isRecord(persistedState)) {
|
||||
return currentState;
|
||||
}
|
||||
const entries = sanitizeQuestionEntries(persistedState.questions);
|
||||
return {
|
||||
...currentState,
|
||||
questions: new Map(entries),
|
||||
};
|
||||
},
|
||||
}
|
||||
),
|
||||
{ name: "question-store" }
|
||||
)
|
||||
);
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Session, Message, Part } from "@opencode-ai/sdk/v2";
|
||||
import type { PermissionRequest, PermissionResponse } from "@/types/permission";
|
||||
import type { QuestionRequest } from "@/types/question";
|
||||
|
||||
export interface AttachedFile {
|
||||
id: string;
|
||||
@@ -78,6 +79,7 @@ export interface SessionStore {
|
||||
messageStreamStates: Map<string, MessageStreamLifecycle>;
|
||||
sessionCompactionUntil: Map<string, number>;
|
||||
permissions: Map<string, PermissionRequest[]>;
|
||||
questions: Map<string, QuestionRequest[]>;
|
||||
sessionAbortFlags: Map<string, { timestamp: number; acknowledged: boolean }>;
|
||||
attachedFiles: AttachedFile[];
|
||||
abortPromptSessionId: string | null;
|
||||
@@ -143,6 +145,12 @@ export interface SessionStore {
|
||||
updateSessionCompaction: (sessionId: string, compactingTimestamp?: number | null) => void;
|
||||
addPermission: (permission: PermissionRequest) => void;
|
||||
respondToPermission: (sessionId: string, requestId: string, response: PermissionResponse) => Promise<void>;
|
||||
|
||||
addQuestion: (question: QuestionRequest) => void;
|
||||
dismissQuestion: (sessionId: string, requestId: string) => void;
|
||||
respondToQuestion: (sessionId: string, requestId: string, answers: string[] | string[][]) => Promise<void>;
|
||||
rejectQuestion: (sessionId: string, requestId: string) => Promise<void>;
|
||||
|
||||
clearError: () => void;
|
||||
getSessionsByDirectory: (directory: string) => Session[];
|
||||
getDirectoryForSession: (sessionId: string) => string | null;
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { StoreApi, UseBoundStore } from "zustand";
|
||||
import { devtools } from "zustand/middleware";
|
||||
import type { Session, Message, Part } from "@opencode-ai/sdk/v2";
|
||||
import type { PermissionRequest, PermissionResponse } from "@/types/permission";
|
||||
import type { QuestionRequest } from "@/types/question";
|
||||
import type { SessionStore, AttachedFile, EditPermissionMode } from "./types/sessionTypes";
|
||||
import { ACTIVE_SESSION_WINDOW, MEMORY_LIMITS } from "./types/sessionTypes";
|
||||
|
||||
@@ -11,6 +12,7 @@ import { useMessageStore } from "./messageStore";
|
||||
import { useFileStore } from "./fileStore";
|
||||
import { useContextStore } from "./contextStore";
|
||||
import { usePermissionStore } from "./permissionStore";
|
||||
import { useQuestionStore } from "./questionStore";
|
||||
import { opencodeClient } from "@/lib/opencode/client";
|
||||
import { useDirectoryStore } from "./useDirectoryStore";
|
||||
import { useConfigStore } from "./useConfigStore";
|
||||
@@ -76,6 +78,7 @@ export const useSessionStore = create<SessionStore>()(
|
||||
sessionCompactionUntil: new Map(),
|
||||
sessionAbortFlags: new Map(),
|
||||
permissions: new Map(),
|
||||
questions: new Map(),
|
||||
attachedFiles: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
@@ -461,6 +464,12 @@ export const useSessionStore = create<SessionStore>()(
|
||||
return usePermissionStore.getState().addPermission(permission, contextData);
|
||||
},
|
||||
respondToPermission: (sessionId: string, requestId: string, response: PermissionResponse) => usePermissionStore.getState().respondToPermission(sessionId, requestId, response),
|
||||
|
||||
addQuestion: (question: QuestionRequest) => useQuestionStore.getState().addQuestion(question),
|
||||
dismissQuestion: (sessionId: string, requestId: string) => useQuestionStore.getState().dismissQuestion(sessionId, requestId),
|
||||
respondToQuestion: (sessionId: string, requestId: string, answers: string[] | string[][]) => useQuestionStore.getState().respondToQuestion(sessionId, requestId, answers),
|
||||
rejectQuestion: (sessionId: string, requestId: string) => useQuestionStore.getState().rejectQuestion(sessionId, requestId),
|
||||
|
||||
clearError: () => useSessionManagementStore.getState().clearError(),
|
||||
getSessionsByDirectory: (directory: string) => useSessionManagementStore.getState().getSessionsByDirectory(directory),
|
||||
getDirectoryForSession: (sessionId: string) => useSessionManagementStore.getState().getDirectoryForSession(sessionId),
|
||||
@@ -876,6 +885,16 @@ usePermissionStore.subscribe((state, prevState) => {
|
||||
});
|
||||
});
|
||||
|
||||
useQuestionStore.subscribe((state, prevState) => {
|
||||
if (state.questions === prevState.questions) {
|
||||
return;
|
||||
}
|
||||
|
||||
useSessionStore.setState({
|
||||
questions: state.questions,
|
||||
});
|
||||
});
|
||||
|
||||
useDirectoryStore.subscribe((state, prevState) => {
|
||||
const nextDirectory = normalizePath(state.currentDirectory ?? null);
|
||||
const prevDirectory = normalizePath(prevState.currentDirectory ?? null);
|
||||
@@ -924,6 +943,7 @@ useSessionStore.setState({
|
||||
lastUsedProvider: useMessageStore.getState().lastUsedProvider,
|
||||
isSyncing: useMessageStore.getState().isSyncing,
|
||||
permissions: usePermissionStore.getState().permissions,
|
||||
questions: useQuestionStore.getState().questions,
|
||||
attachedFiles: useFileStore.getState().attachedFiles,
|
||||
sessionModelSelections: useContextStore.getState().sessionModelSelections,
|
||||
sessionAgentSelections: useContextStore.getState().sessionAgentSelections,
|
||||
|
||||
Reference in New Issue
Block a user