Official
chat
Built-in chat UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/chat@0.2.6
Permissions in 0.2.6
Files
answers.ts3.4 KB
// Answers typed into a question card survive a plugin reload, as unsent
// prompts do: plugin storage keeps them per chat until the question goes.
import * as Effect from "effect/Effect";
import * as Semaphore from "effect/Semaphore";
import * as z from "zod";
import { parse } from "divergence/effect";
import type { Controller } from "./controller.ts";
import { errorMessage } from "./data.ts";
// A field's answer is a string, a string array or a boolean (QuestionAnswer).
const stored = z.record(z.string(), z.record(z.string(), z.union([z.string(), z.boolean(), z.array(z.string())])));
type Stored = z.infer<typeof stored>;
const answersKey = (chatId: string) => `answers:${chatId}`;
export class Answers {
// One write at a time: storage writes may otherwise land out of order.
private readonly lane = Semaphore.makeUnsafe(1);
private readonly dirty = new Set<string>();
private readonly restored = new Set<string>();
readonly controller: Controller;
constructor(controller: Controller) {
this.controller = controller;
}
/// The answers this chat had before the plugin started, once per chat:
/// afterwards the ones in memory are newer.
read = Effect.fn("Answers.read")((chatId: string) =>
this.restored.has(chatId)
? Effect.succeed(undefined)
: this.controller.host.call("host/storage.get", { key: answersKey(chatId) }).pipe(
Effect.flatMap(({ value }) => parse("question answers", stored, value ?? {})),
Effect.catch((error) =>
this.controller.notify
.error(`Typed answers could not be restored: ${errorMessage(error)}`)
.pipe(Effect.as(undefined)),
),
),
);
/// Installs what `read` found beside the transcript's pending questions.
restore = Effect.fn("Answers.restore")((chatId: string, saved: Stored | undefined) =>
Effect.suspend(() => {
if (saved) {
this.restored.add(chatId);
const transcript = this.controller.transcript(chatId);
for (const [id, answers] of Object.entries(saved)) transcript.answers[id] ??= answers;
}
// Forgetting stale answers in storage must not hold the transcript's paint.
return Effect.forkIn(this.prune(chatId), this.controller.scope, { startImmediately: true });
}),
);
/// A question answered, withdrawn or ended with its turn takes its answers along.
prune = Effect.fn("Answers.prune")((chatId: string) =>
Effect.suspend(() => {
const transcript = this.controller.state.transcripts[chatId];
if (!transcript) return Effect.void;
const gone = Object.keys(transcript.answers).filter((id) => !transcript.questions.some((q) => q.id === id));
if (!gone.length) return Effect.void;
for (const id of gone) delete transcript.answers[id];
return this.save(chatId);
}),
);
save = Effect.fn("Answers.save")((chatId: string) =>
Effect.suspend(() => {
this.dirty.add(chatId);
return this.lane.withPermit(
Effect.suspend(() => {
// A write queued behind one that already took the latest answers.
if (!this.dirty.delete(chatId)) return Effect.void;
return this.controller.host.call("host/storage.set", {
key: answersKey(chatId),
value: this.controller.state.transcripts[chatId]?.answers ?? {},
});
}),
);
}).pipe(
Effect.catch((error) => this.controller.notify.error(`Typed answers could not be saved: ${errorMessage(error)}`)),
),
);
}Versions
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.2.9latest | Nov 21, 2026 | >=5 <6 | 349 KB | 5 permissions | Listed |
| 0.2.8 | Nov 16, 2026 | >=5 <6 | 348.6 KB | 5 permissions | Listed |
| 0.2.7 | Nov 11, 2026 | >=5 <6 | 314.3 KB | 5 permissions | Listed |
| 0.2.6 | Nov 9, 2026 | >=5 <6 | 306.2 KB | 5 permissions | Listed |
| 0.2.5 | Oct 27, 2026 | >=2 <4 | 178.6 KB | 5 permissions | Listed |
No comments yet.