add quiz service
This commit is contained in:
parent
fe4ec8ebcd
commit
6d082f2ff7
6 changed files with 130 additions and 0 deletions
86
extensions/cli/src/quiz/QuizService.ts
Normal file
86
extensions/cli/src/quiz/QuizService.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import { EventEmitter } from "events";
|
||||
|
||||
import {
|
||||
PendingQuestion,
|
||||
QuestionAnsweredEvent,
|
||||
QuestionRequestedEvent,
|
||||
QuizQuestion,
|
||||
QuizServiceState,
|
||||
} from "./types.js";
|
||||
|
||||
export class QuizService extends EventEmitter {
|
||||
private pendingRequests = new Map<
|
||||
string,
|
||||
{
|
||||
question: QuizQuestion;
|
||||
resolve: (answer: string) => void;
|
||||
}
|
||||
>();
|
||||
|
||||
private currentState: QuizServiceState = {
|
||||
pendingQuestion: null,
|
||||
};
|
||||
|
||||
/**set a pending question to be answered and return a promise that resolves when the question is answered */
|
||||
async askQuestion(question: QuizQuestion): Promise<string> {
|
||||
const requestId = `quiz-${Date.now()}`;
|
||||
|
||||
return new Promise<string>((resolve) => {
|
||||
const pendingQuestion: PendingQuestion = {
|
||||
...question,
|
||||
requestId,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
this.pendingRequests.set(requestId, {
|
||||
question,
|
||||
resolve,
|
||||
});
|
||||
|
||||
this.currentState.pendingQuestion = pendingQuestion;
|
||||
|
||||
const event: QuestionRequestedEvent = {
|
||||
requestId,
|
||||
question,
|
||||
};
|
||||
|
||||
this.emit("questionRequested", event);
|
||||
});
|
||||
}
|
||||
|
||||
/**answer the pending question request and resolve the answer */
|
||||
answerQuestion(
|
||||
requestId: string,
|
||||
answer: string,
|
||||
isCustomAnswer = false,
|
||||
): boolean {
|
||||
const pending = this.pendingRequests.get(requestId);
|
||||
if (!pending) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.pendingRequests.delete(requestId);
|
||||
this.currentState.pendingQuestion = null;
|
||||
|
||||
const event: QuestionAnsweredEvent = {
|
||||
requestId,
|
||||
answer,
|
||||
isCustomAnswer,
|
||||
};
|
||||
|
||||
this.emit("questionAnswered", event);
|
||||
pending.resolve(answer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async initialize(): Promise<QuizServiceState> {
|
||||
this.currentState = {
|
||||
pendingQuestion: null,
|
||||
};
|
||||
this.pendingRequests.clear();
|
||||
return this.currentState;
|
||||
}
|
||||
}
|
||||
|
||||
export const quizService = new QuizService();
|
||||
2
extensions/cli/src/quiz/index.ts
Normal file
2
extensions/cli/src/quiz/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export { QuizService, quizService } from "./QuizService.js";
|
||||
export * from "./types.js";
|
||||
32
extensions/cli/src/quiz/types.ts
Normal file
32
extensions/cli/src/quiz/types.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
export interface QuizQuestion {
|
||||
question: string;
|
||||
options?: string[];
|
||||
allowCustomAnswer?: boolean;
|
||||
defaultAnswer?: string;
|
||||
}
|
||||
|
||||
export interface PendingQuestion extends QuizQuestion {
|
||||
requestId: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface QuizAnswer {
|
||||
requestId: string;
|
||||
answer: string;
|
||||
isCustomAnswer: boolean;
|
||||
}
|
||||
|
||||
export interface QuizServiceState {
|
||||
pendingQuestion: PendingQuestion | null;
|
||||
}
|
||||
|
||||
export interface QuestionRequestedEvent {
|
||||
requestId: string;
|
||||
question: QuizQuestion;
|
||||
}
|
||||
|
||||
export interface QuestionAnsweredEvent {
|
||||
requestId: string;
|
||||
answer: string;
|
||||
isCustomAnswer: boolean;
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { loadAuthConfig } from "../auth/workos.js";
|
||||
import { initializeWithOnboarding } from "../onboarding.js";
|
||||
import { quizService } from "../quiz/index.js";
|
||||
import {
|
||||
setBetaSubagentToolEnabled,
|
||||
setBetaUploadArtifactToolEnabled,
|
||||
|
|
@ -325,6 +326,12 @@ export async function initializeServices(initOptions: ServiceInitOptions = {}) {
|
|||
[], // No dependencies
|
||||
);
|
||||
|
||||
serviceContainer.register(
|
||||
SERVICE_NAMES.QUIZ,
|
||||
() => quizService.initialize(),
|
||||
[], // No dependencies
|
||||
);
|
||||
|
||||
// Eagerly initialize all services to ensure they're ready when needed
|
||||
// This avoids race conditions and "service not ready" errors
|
||||
await serviceContainer.initializeAll();
|
||||
|
|
@ -389,6 +396,7 @@ export const services = {
|
|||
artifactUpload: artifactUploadService,
|
||||
gitAiIntegration: gitAiIntegrationService,
|
||||
backgroundJobs: backgroundJobService,
|
||||
quiz: quizService,
|
||||
} as const;
|
||||
|
||||
export type ServicesType = typeof services;
|
||||
|
|
|
|||
|
|
@ -158,6 +158,7 @@ export const SERVICE_NAMES = {
|
|||
ARTIFACT_UPLOAD: "artifactUpload",
|
||||
GIT_AI_INTEGRATION: "gitAiIntegration",
|
||||
BACKGROUND_JOBS: "backgroundJobs",
|
||||
QUIZ: "quiz",
|
||||
} as const;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
* When adding a new built-in tool, update both this list and ALL_BUILT_IN_TOOLS in allBuiltIns.ts.
|
||||
*/
|
||||
export const BUILT_IN_TOOL_NAMES = [
|
||||
"AskQuestion",
|
||||
"Edit",
|
||||
"Exit",
|
||||
"Fetch",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue