Official
market
The plugin marketplace: browse, install through an agent's scan, update and publish plugins, and the marketplace tools of agents in the Plugins workspace.
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/market@0.1.6
Permissions in 0.1.6
Files
account.ts8.2 KB
// The marketplace account in the page's header and the sidebar's Options
// menu: reading it, the device sign-in with its code, cancelling it, signing
// out, the profile page (claiming a username, saving the fields, the
// picture), the account's page on the website, and the picture in the
// sidebar's Options circle.
import * as Effect from "effect/Effect";
import { App, Host, Notify, Plugin, parse, promise } from "divergence/effect";
import type { HostError, PluginServices } from "divergence/effect";
import { MarketState, PICTURE_ITEM, ROUTE } from "./state.ts";
import { messageOf, profileChanges, profileEditable, profileUrl } from "./logic.ts";
import type { ProfileEdits, ProfileField } from "./logic.ts";
import { accountSchema, loginSchema } from "./remote.ts";
// A host that does not know a method answers with JSON-RPC's
// method-not-found code, or, before it had one for host methods, with an
// error that names the method as unknown.
const unknownMethod = (error: HostError): boolean =>
error._tag === "HostCallFailed" && (error.code === -32601 || error.message.startsWith("unknown host method"));
export const accountActions = Effect.fn("Market.accountActions")(function* (
redraw: () => void,
fail: (what: string, error: unknown) => Effect.Effect<void>,
busy: (
key: string,
work: Effect.Effect<unknown, unknown, PluginServices>,
) => Effect.Effect<void, unknown, PluginServices>,
) {
const state = yield* MarketState;
const host = yield* Host;
const notify = yield* Notify;
const plugin = yield* Plugin;
const app = yield* App;
// The sidebar draws the picture's view only while there is a picture, and
// its person mark otherwise.
let published: number | null = null;
const publishPicture = Effect.fn("Market.publishPicture")(function* () {
const account = state.account;
const view = account?.signedIn && "picture" in account && account.picture ? state.pictureView : null;
if (view === published) return;
published = view;
yield* app.update({ shared: { [PICTURE_ITEM]: view } });
});
const loadAccount = Effect.fn("Market.loadAccount")(function* () {
yield* host.call("host/market.whoami", {}).pipe(
Effect.flatMap((raw) => parse("account", accountSchema, raw)),
Effect.tap((account) =>
Effect.sync(() => {
state.account = account;
}),
),
Effect.catch((error) =>
Effect.sync(() => {
state.account = { signedIn: false as const, error: messageOf(error) };
}),
),
);
redraw();
yield* publishPicture();
return state.account;
});
const registry = Effect.fn("Market.registry")(function* () {
return (state.account ?? (yield* loadAccount()))?.registry;
});
// The page's Sign in shows the code and opens the browser only on its own
// click; the sidebar's opens it at once (`open`), since its one row was the
// click that asked to sign in.
const signIn = Effect.fn("Market.signIn")(function* (open = false) {
yield* busy(
"sign-in",
host.call("host/market.login", {}).pipe(
Effect.flatMap((raw) => parse("sign-in", loginSchema, raw)),
Effect.tap((code) =>
Effect.sync(() => {
state.signIn = { code: code.userCode, url: code.verificationUriComplete ?? code.verificationUri };
}),
),
Effect.andThen(() => (open ? openSignInPage() : Effect.void)),
Effect.catch((error) => fail("Could not start the sign-in", error)),
),
);
});
// The code row's X: the host stops waiting for the code, so a page
// still open in the browser can no longer sign the app in. An older host
// has no such call (method not found); its wait ends when the code expires.
const cancelSignIn = Effect.fn("Market.cancelSignIn")(function* () {
state.signIn = null;
redraw();
yield* host
.call("host/market.login_cancel", {})
.pipe(
Effect.catch((error) => (unknownMethod(error) ? Effect.void : fail("Could not cancel the sign-in", error))),
);
});
const signOut = Effect.fn("Market.signOut")(function* () {
yield* busy(
"sign-out",
Effect.gen(function* () {
yield* host.call("host/market.logout", {}).pipe(Effect.catch((error) => fail("Could not sign out", error)));
yield* loadAccount();
}),
);
});
const openSignInPage = Effect.fn("Market.openSignInPage")(function* () {
const signInState = state.signIn;
if (!signInState) return;
const result = yield* promise("openUrl", () => plugin.api.openUrl(signInState.url));
if (!result.opened)
yield* notify.warning(
`Open ${signInState.url} and enter ${signInState.code}${result.reason ? ` (${result.reason})` : ""}.`,
);
});
const openProfileSite = Effect.fn("Market.openProfileSite")(function* () {
const account = state.account;
if (!account?.signedIn) return;
yield* promise("openUrl", () => plugin.api.openUrl(profileUrl(account.registry, "account" in account ? account.account : undefined)));
});
// The sidebar's account row: the profile page, or the website when the
// host cannot edit the profile.
const openProfile = Effect.fn("Market.openProfile")(function* () {
if (!profileEditable(state.account)) return yield* openProfileSite();
state.page = "profile";
redraw();
yield* app.route(ROUTE);
});
const publisher = () => {
const account = state.account;
return account?.signedIn && "account" in account ? account.account?.publisher : undefined;
};
const editProfile = (key: ProfileField | "creditOptOut", value: string | boolean): void => {
state.profileEdits = { ...state.profileEdits, [key]: value };
redraw();
};
const setClaimName = (name: string): void => {
state.claimName = name;
redraw();
};
// Each change reads the account again, so the page, the sidebar's row and
// the picture in its Options circle show what the registry now holds.
const change = (key: string, call: Effect.Effect<unknown, HostError>, what: string, after = (): void => {}) =>
busy(
key,
call.pipe(
Effect.andThen(() => loadAccount()),
Effect.tap(() => Effect.sync(after)),
Effect.catch((error) => fail(what, error)),
),
);
// Only the edits that were saved are dropped: what was typed while the
// save was on its way stays on the page, still to save.
const saveProfile = Effect.fn("Market.saveProfile")(function* () {
const sent = state.profileEdits;
const fields = profileChanges(publisher(), sent);
yield* change("profile-save", host.call("host/market.update_profile", { fields }), "Could not save the profile", () => {
state.profileEdits = Object.fromEntries(
Object.entries(state.profileEdits).filter(([key, value]) => sent[key as keyof ProfileEdits] !== value),
);
});
});
const claimUsername = Effect.fn("Market.claimUsername")(function* () {
const name = state.claimName.trim();
yield* change("claim", host.call("host/market.claim_username", { name }), "Could not claim the username", () => {
state.claimName = "";
});
});
const setPicture = (path: string | null) =>
change(
"picture",
host.call("host/market.set_avatar", { path }),
path ? "Could not upload the picture" : "Could not remove the picture",
);
const uploadPicture = Effect.fn("Market.uploadPicture")(function* () {
const { paths } = yield* promise("pickImages", () => plugin.api.pickImages());
const [path] = paths;
if (path) yield* setPicture(path);
});
const removePicture = Effect.fn("Market.removePicture")(function* () {
yield* setPicture(null);
});
const onMarketAccount = Effect.fn("Market.onMarketAccount")(function* (event: {
readonly signedIn: boolean;
readonly error?: string;
}) {
state.signIn = null;
if (event.error) yield* notify.error(`Marketplace sign-in: ${event.error}`);
else if (event.signedIn) yield* notify.info("Signed in to the marketplace.");
yield* loadAccount();
});
return {
loadAccount,
registry,
signIn,
cancelSignIn,
signOut,
openSignInPage,
openProfile,
openProfileSite,
editProfile,
setClaimName,
saveProfile,
claimUsername,
uploadPicture,
removePicture,
onMarketAccount,
};
});Versions
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.1.6latest | Nov 21, 2026 | >=5 <6 | 71.9 KB | 6 permissions | Listed |
| 0.1.5 | Nov 18, 2026 | >=5 <6 | 71.4 KB | 6 permissions | Listed |
| 0.1.4 | Nov 16, 2026 | >=5 <6 | 70.5 KB | 6 permissions | Listed |
| 0.1.3 | Nov 11, 2026 | >=5 <6 | 62.7 KB | 6 permissions | Listed |
| 0.1.2 | Nov 9, 2026 | >=5 <6 | 57.5 KB | 6 permissions | Listed |
| 0.1.1 | Oct 27, 2026 | >=2 <4 | 46.2 KB | 6 permissions | Listed |
No comments yet.