@ubiquity-os/plugin-sdk
Version:
SDK for plugin support.
111 lines (108 loc) • 3.59 kB
JavaScript
// src/types/manifest.ts
import { Type as T } from "@sinclair/typebox";
import { emitterEventNames } from "@octokit/webhooks";
// src/helpers/runtime-manifest.ts
var EMPTY_VALUE = String();
var GITHUB_HEADS_PREFIX = "refs/heads/";
var GITHUB_TAGS_PREFIX = "refs/tags/";
function readRuntimeEnvValue(env, key) {
const explicitValue = env?.[key];
if (typeof explicitValue === "string" && explicitValue.trim()) {
return explicitValue.trim();
}
const runtime = globalThis;
if (typeof runtime.Deno?.env?.get === "function") {
const denoValue = runtime.Deno.env.get(key);
if (typeof denoValue === "string" && denoValue.trim()) {
return denoValue.trim();
}
}
if (typeof process !== "undefined") {
const processValue = process.env[key];
if (typeof processValue === "string" && processValue.trim()) {
return processValue.trim();
}
}
return EMPTY_VALUE;
}
function parseRefNameFromGitHubRef(ref) {
if (!ref) {
return EMPTY_VALUE;
}
if (ref.startsWith(GITHUB_HEADS_PREFIX)) {
return ref.slice(GITHUB_HEADS_PREFIX.length);
}
if (ref.startsWith(GITHUB_TAGS_PREFIX)) {
return ref.slice(GITHUB_TAGS_PREFIX.length);
}
return EMPTY_VALUE;
}
function overrideShortName(shortName, refName) {
if (!shortName || !refName) {
return shortName;
}
const separatorIndex = shortName.lastIndexOf("@");
const repository = separatorIndex === -1 ? shortName : shortName.slice(0, separatorIndex);
if (!repository) {
return shortName;
}
return `${repository}@${refName}`;
}
function resolveRuntimeRefName(env) {
const explicitRefName = readRuntimeEnvValue(env, "REF_NAME");
if (explicitRefName) {
return explicitRefName;
}
const legacyManifestRefName = readRuntimeEnvValue(env, "PLUGIN_MANIFEST_REF_NAME");
if (legacyManifestRefName) {
return legacyManifestRefName;
}
const githubRefName = readRuntimeEnvValue(env, "GITHUB_REF_NAME");
if (githubRefName) {
return githubRefName;
}
return parseRefNameFromGitHubRef(readRuntimeEnvValue(env, "GITHUB_REF"));
}
function resolveRuntimeManifest(manifest, env) {
const refName = resolveRuntimeRefName(env);
if (!refName) {
return manifest;
}
return {
...manifest,
short_name: overrideShortName(manifest.short_name, refName)
};
}
// src/types/manifest.ts
var runEvent = T.Union(emitterEventNames.map((o) => T.Literal(o)));
var exampleCommandExecutionSchema = T.Object({
commandInvocation: T.String({ minLength: 1 }),
githubContext: T.Optional(T.Record(T.String(), T.Any())),
expectedToolCallResult: T.Object({
function: T.String({ minLength: 1 }),
parameters: T.Record(T.String(), T.Any())
})
});
var commandSchema = T.Object({
description: T.String({ minLength: 1 }),
"ubiquity:example": T.String({ minLength: 1 }),
parameters: T.Optional(T.Record(T.String(), T.Any())),
examples: T.Optional(T.Array(exampleCommandExecutionSchema, { default: [] }))
});
var manifestSchema = T.Object({
name: T.String({ minLength: 1 }),
short_name: T.String({ minLength: 1 }),
description: T.Optional(T.String({ default: "" })),
commands: T.Optional(T.Record(T.String({ pattern: "^[A-Za-z-_]+$" }), commandSchema, { default: {} })),
"ubiquity:listeners": T.Optional(T.Array(runEvent, { default: [] })),
configuration: T.Optional(T.Record(T.String(), T.Any(), { default: {} })),
skipBotEvents: T.Optional(T.Boolean({ default: true })),
homepage_url: T.Optional(T.String())
});
export {
commandSchema,
exampleCommandExecutionSchema,
manifestSchema,
resolveRuntimeManifest,
runEvent
};