GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode/packages/opencode/src/question/index.ts at dev · ildella/opencode Expand file tree/ Copy pathindex.tsMore file actions 229 lines (197 loc) · 7.53 KB / Copy pathindex.tsFile metadata and controls229 lines (197 loc) · 7.53 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { Deferred, Effect, Layer, Schema, Context } from "effect" import { InstanceState } from "@/effect/instance-state" import { SessionID, MessageID } from "@/session/schema" import { QuestionID } from "./schema" import { EventV2Bridge } from "@/event-v2-bridge" import { EventV2 } from "@opencode-ai/core/event" // Schemas — these are pure data; nothing checks class identity (see PR // description) so they're plain `Schema.Struct` + type alias. That lets // `Question.ask` and other internal sites trust the type contract without a // re-decode to coerce nested class instances. export const Option = Schema.Struct({ label: Schema.String.annotate({ description: "Display text (1-5 words, concise)", }), description: Schema.String.annotate({ description: "Explanation of choice", }), }).annotate({ identifier: "QuestionOption" }) export type Option = Schema.Schema.Type<typeof Option> const base = { question: Schema.String.annotate({ description: "Complete question", }), header: Schema.String.annotate({ description: "Very short label (max 30 chars)", }), options: Schema.Array(Option).annotate({ description: "Available choices", }), multiple: Schema.optional(Schema.Boolean).annotate({ description: "Allow selecting multiple choices", }), } export const Info = Schema.Struct({ ...base, custom: Schema.optional(Schema.Boolean).annotate({ description: "Allow typing a custom answer (default: true)", }), }).annotate({ identifier: "QuestionInfo" }) export type Info = Schema.Schema.Type<typeof Info> export const Prompt = Schema.Struct(base).annotate({ identifier: "QuestionPrompt" }) export type Prompt = Schema.Schema.Type<typeof Prompt> export const Tool = Schema.Struct({ messageID: MessageID, callID: Schema.String, }).annotate({ identifier: "QuestionTool" }) export type Tool = Schema.Schema.Type<typeof Tool> export const Request = Schema.Struct({ id: QuestionID, sessionID: SessionID, questions: Schema.Array(Info).annotate({ description: "Questions to ask", }), tool: Schema.optional(Tool), }).annotate({ identifier: "QuestionRequest" }) export type Request = Schema.Schema.Type<typeof Request> export const Answer = Schema.Array(Schema.String).annotate({ identifier: "QuestionAnswer" }) export type Answer = Schema.Schema.Type<typeof Answer> export const Reply = Schema.Struct({ answers: Schema.Array(Answer).annotate({ description: "User answers in order of questions (each answer is an array of selected labels)", }), }).annotate({ identifier: "QuestionReply" }) export type Reply = Schema.Schema.Type<typeof Reply> export const Replied = Schema.Struct({ sessionID: SessionID, requestID: QuestionID, answers: Schema.Array(Answer), }).annotate({ identifier: "QuestionReplied" }) export const Rejected = Schema.Struct({ sessionID: SessionID, requestID: QuestionID, }).annotate({ identifier: "QuestionRejected" }) export const Event = { Asked: EventV2.define({ type: "question.asked", schema: Request.fields }), Replied: EventV2.define({ type: "question.replied", schema: Replied.fields }), Rejected: EventV2.define({ type: "question.rejected", schema: Rejected.fields }), } export class RejectedError extends Schema.TaggedErrorClass<RejectedError>()("QuestionRejectedError", {}) { override get message() { return "The user dismissed this question" } } export class NotFoundError extends Schema.TaggedErrorClass<NotFoundError>()("Question.NotFoundError", { requestID: QuestionID, }) {} interface PendingEntry { info: Request deferred: Deferred.Deferred<ReadonlyArray<Answer>, RejectedError> } interface State { pending: Map<QuestionID, PendingEntry> } // Service export interface Interface { readonly ask: (input: { sessionID: SessionID questions: ReadonlyArray<Info> tool?: Tool }) => Effect.Effect<ReadonlyArray<Answer>, RejectedError> readonly reply: (input: { requestID: QuestionID answers: ReadonlyArray<Answer> }) => Effect.Effect<void, NotFoundError> readonly reject: (requestID: QuestionID) => Effect.Effect<void, NotFoundError> readonly list: () => Effect.Effect<ReadonlyArray<Request>> } export class Service extends Context.Service<Service, Interface>()("@opencode/Question") {} export const layer = Layer.effect( Service, Effect.gen(function* () { const events = yield* EventV2Bridge.Service const state = yield* InstanceState.make<State>( Effect.fn("Question.state")(function* () { const state = { pending: new Map<QuestionID, PendingEntry>(), } yield* Effect.addFinalizer(() => Effect.gen(function* () { for (const item of state.pending.values()) { yield* Deferred.fail(item.deferred, new RejectedError()) } state.pending.clear() }), ) return state }), ) const ask = Effect.fn("Question.ask")(function* (input: { sessionID: SessionID questions: ReadonlyArray<Info> tool?: Tool }) { const pending = (yield* InstanceState.get(state)).pending const id = QuestionID.ascending() yield* Effect.logInfo("asking", { id, questions: input.questions.length }) const deferred = yield* Deferred.make<ReadonlyArray<Answer>, RejectedError>() const info: Request = { id, sessionID: input.sessionID, questions: input.questions, tool: input.tool, } pending.set(id, { info, deferred }) yield* events.publish(Event.Asked, info) return yield* Effect.ensuring( Deferred.await(deferred), Effect.sync(() => { pending.delete(id) }), ) }) const reply = Effect.fn("Question.reply")(function* (input: { requestID: QuestionID answers: ReadonlyArray<Answer> }) { const pending = (yield* InstanceState.get(state)).pending const existing = pending.get(input.requestID) if (!existing) { yield* Effect.logWarning("reply for unknown request", { requestID: input.requestID }) return yield* new NotFoundError({ requestID: input.requestID }) } pending.delete(input.requestID) yield* Effect.logInfo("replied", { requestID: input.requestID, answers: input.answers }) yield* events.publish(Event.Replied, { sessionID: existing.info.sessionID, requestID: existing.info.id, answers: input.answers.map((a) => [...a]), }) yield* Deferred.succeed(existing.deferred, input.answers) }) const reject = Effect.fn("Question.reject")(function* (requestID: QuestionID) { const pending = (yield* InstanceState.get(state)).pending const existing = pending.get(requestID) if (!existing) { yield* Effect.logWarning("reject for unknown request", { requestID }) return yield* new NotFoundError({ requestID }) } pending.delete(requestID) yield* Effect.logInfo("rejected", { requestID }) yield* events.publish(Event.Rejected, { sessionID: existing.info.sessionID, requestID: existing.info.id, }) yield* Deferred.fail(existing.deferred, new RejectedError()) }) const list = Effect.fn("Question.list")(function* () { const pending = (yield* InstanceState.get(state)).pending return Array.from(pending.values(), (x) => x.info) }) return Service.of({ ask, reply, reject, list }) }), ) export const defaultLayer = layer.pipe(Layer.provide(EventV2Bridge.defaultLayer)) export const node = LayerNode.make(layer, [EventV2Bridge.node]) export * as Question from "." |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |