personify-cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +98 -2
- package/dist/args.js +52 -0
- package/dist/cli.js +8 -0
- package/dist/commands.js +502 -0
- package/dist/config.js +73 -0
- package/dist/errors.js +44 -0
- package/dist/help.js +34 -0
- package/dist/main.js +101 -0
- package/dist/mcp.js +90 -0
- package/dist/oauth.js +205 -0
- package/dist/output.js +45 -0
- package/dist/session.js +89 -0
- package/package.json +43 -4
package/dist/main.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { parseArgs, stringFlag } from "./args.js";
|
|
3
|
+
import { COMMANDS, findCommand } from "./commands.js";
|
|
4
|
+
import { CliError, EXIT } from "./errors.js";
|
|
5
|
+
import { help } from "./help.js";
|
|
6
|
+
import { openBrowser } from "./oauth.js";
|
|
7
|
+
import { formatOutput } from "./output.js";
|
|
8
|
+
export const VERSION = createRequire(import.meta.url)("../package.json").version;
|
|
9
|
+
function readStdinLine() {
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
let data = "";
|
|
12
|
+
process.stdin.setEncoding("utf8");
|
|
13
|
+
process.stdin.on("data", (chunk) => {
|
|
14
|
+
data += chunk;
|
|
15
|
+
if (data.includes("\n")) {
|
|
16
|
+
process.stdin.pause();
|
|
17
|
+
resolve(data.split("\n")[0]);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
process.stdin.on("end", () => resolve(data));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export function normalizeOrigin(raw) {
|
|
24
|
+
try {
|
|
25
|
+
return new URL(raw).origin;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new CliError(`Not a valid origin: ${raw}`, EXIT.USAGE, "bad_origin");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export async function main(argv, deps = {}) {
|
|
32
|
+
const env = deps.env ?? process.env;
|
|
33
|
+
const stdout = deps.stdout ?? ((line) => process.stdout.write(line + "\n"));
|
|
34
|
+
const stderr = deps.stderr ?? ((line) => process.stderr.write(line + "\n"));
|
|
35
|
+
const args = parseArgs(argv);
|
|
36
|
+
const json = args.flags.json === true || env.PERSONIFY_JSON === "1";
|
|
37
|
+
try {
|
|
38
|
+
if (args.flags.version === true || args.positionals[0] === "version") {
|
|
39
|
+
stdout(json ? JSON.stringify({ version: VERSION }) : VERSION);
|
|
40
|
+
return EXIT.OK;
|
|
41
|
+
}
|
|
42
|
+
if (!args.positionals.length ||
|
|
43
|
+
args.flags.help === true ||
|
|
44
|
+
args.positionals[0] === "help") {
|
|
45
|
+
const topic = args.positionals[0] === "help"
|
|
46
|
+
? args.positionals.slice(1).join(" ")
|
|
47
|
+
: args.positionals.join(" ");
|
|
48
|
+
stdout(json
|
|
49
|
+
? JSON.stringify({
|
|
50
|
+
version: VERSION,
|
|
51
|
+
commands: COMMANDS.map((c) => ({
|
|
52
|
+
name: c.name,
|
|
53
|
+
usage: c.usage,
|
|
54
|
+
summary: c.summary,
|
|
55
|
+
})),
|
|
56
|
+
}, null, 2)
|
|
57
|
+
: help(topic || undefined));
|
|
58
|
+
return EXIT.OK;
|
|
59
|
+
}
|
|
60
|
+
const found = findCommand(args.positionals);
|
|
61
|
+
if (!found)
|
|
62
|
+
throw new CliError(`Unknown command: ${args.positionals.slice(0, 2).join(" ")}. Run \`personify --help\`.`, EXIT.USAGE, "usage");
|
|
63
|
+
const origin = normalizeOrigin(stringFlag(args.flags, "api-url") ||
|
|
64
|
+
env.PERSONIFY_API_URL ||
|
|
65
|
+
"https://personify.gg");
|
|
66
|
+
const ctx = {
|
|
67
|
+
args: { ...args, positionals: args.positionals.slice(found.depth) },
|
|
68
|
+
origin,
|
|
69
|
+
json,
|
|
70
|
+
env,
|
|
71
|
+
fetchImpl: deps.fetchImpl ?? fetch,
|
|
72
|
+
now: deps.now ?? Date.now,
|
|
73
|
+
openBrowser: deps.openBrowser ?? openBrowser,
|
|
74
|
+
readStdin: deps.readStdin ?? readStdinLine,
|
|
75
|
+
log: stderr,
|
|
76
|
+
sleep: deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))),
|
|
77
|
+
};
|
|
78
|
+
const outcome = await found.command.run(ctx);
|
|
79
|
+
stdout(formatOutput(outcome.data, json));
|
|
80
|
+
if (!json && outcome.text)
|
|
81
|
+
stderr(outcome.text);
|
|
82
|
+
return outcome.exit ?? EXIT.OK;
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
const err = e instanceof CliError
|
|
86
|
+
? e
|
|
87
|
+
: new CliError(e instanceof Error ? e.message : String(e), EXIT.ERROR, "error");
|
|
88
|
+
if (json)
|
|
89
|
+
stdout(JSON.stringify({
|
|
90
|
+
error: err.message,
|
|
91
|
+
code: err.code,
|
|
92
|
+
exit: err.exit,
|
|
93
|
+
...(err.details && typeof err.details === "object"
|
|
94
|
+
? { details: err.details }
|
|
95
|
+
: {}),
|
|
96
|
+
}, null, 2));
|
|
97
|
+
else
|
|
98
|
+
stderr(`Error: ${err.message}`);
|
|
99
|
+
return err.exit;
|
|
100
|
+
}
|
|
101
|
+
}
|
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CliError, EXIT, exitForMessage } from "./errors.js";
|
|
2
|
+
export const PROTOCOL_VERSION = "2025-06-18";
|
|
3
|
+
/** Parse a JSON or SSE JSON-RPC response body. */
|
|
4
|
+
export function parseRpc(raw, contentType) {
|
|
5
|
+
const text = raw.trim();
|
|
6
|
+
if (!text)
|
|
7
|
+
return null;
|
|
8
|
+
if (contentType.includes("text/event-stream")) {
|
|
9
|
+
const data = text
|
|
10
|
+
.split(/\r?\n/)
|
|
11
|
+
.filter((line) => line.startsWith("data:"))
|
|
12
|
+
.map((line) => line.slice(5).trim())
|
|
13
|
+
.filter(Boolean);
|
|
14
|
+
for (const chunk of data.reverse()) {
|
|
15
|
+
try {
|
|
16
|
+
const message = JSON.parse(chunk);
|
|
17
|
+
if (message.result || message.error)
|
|
18
|
+
return message;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// Keep looking for the response event.
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(text);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export async function rpc(session, method, params, fetchImpl = fetch) {
|
|
34
|
+
let res;
|
|
35
|
+
try {
|
|
36
|
+
res = await fetchImpl(`${session.origin}/api/mcp`, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
Accept: "application/json, text/event-stream",
|
|
40
|
+
"Content-Type": "application/json",
|
|
41
|
+
Authorization: `Bearer ${session.token}`,
|
|
42
|
+
"MCP-Protocol-Version": PROTOCOL_VERSION,
|
|
43
|
+
"User-Agent": "personify-cli",
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
throw new CliError(`Could not reach ${session.origin}. Check the network or PERSONIFY_API_URL.`, EXIT.UNAVAILABLE, "network");
|
|
50
|
+
}
|
|
51
|
+
if (res.status === 401)
|
|
52
|
+
throw new CliError("Not signed in, or the session expired. Run `personify login`.", EXIT.AUTH, "unauthenticated");
|
|
53
|
+
if (res.status === 429)
|
|
54
|
+
throw new CliError("Too many requests. Try again shortly.", EXIT.UNAVAILABLE, "rate_limited");
|
|
55
|
+
const message = parseRpc(await res.text(), res.headers.get("content-type") || "");
|
|
56
|
+
if (!message)
|
|
57
|
+
throw new CliError(res.ok
|
|
58
|
+
? "Personify sent an unreadable response."
|
|
59
|
+
: `Personify returned ${res.status}.`, EXIT.UNAVAILABLE, "bad_response");
|
|
60
|
+
if (message.error)
|
|
61
|
+
throw new CliError(message.error.message, EXIT.ERROR, "rpc_error");
|
|
62
|
+
return message.result || {};
|
|
63
|
+
}
|
|
64
|
+
/** Call one MCP tool and return its JSON payload. A refusal becomes a CliError. */
|
|
65
|
+
export async function callTool(session, name, args = {}, fetchImpl = fetch) {
|
|
66
|
+
const result = (await rpc(session, "tools/call", { name, arguments: args }, fetchImpl));
|
|
67
|
+
const text = result.content?.find((c) => c.type === "text")?.text ?? "";
|
|
68
|
+
let data = text;
|
|
69
|
+
try {
|
|
70
|
+
data = JSON.parse(text);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// Plain text stays as is.
|
|
74
|
+
}
|
|
75
|
+
if (result.isError) {
|
|
76
|
+
const message = data && typeof data === "object" && "error" in data
|
|
77
|
+
? String(data.error)
|
|
78
|
+
: text || "The tool call failed.";
|
|
79
|
+
throw new CliError(message, exitForMessage(message), "tool_error", data);
|
|
80
|
+
}
|
|
81
|
+
return data;
|
|
82
|
+
}
|
|
83
|
+
export async function listTools(session, fetchImpl = fetch) {
|
|
84
|
+
const result = await rpc(session, "tools/list", {}, fetchImpl);
|
|
85
|
+
return (result.tools || []).map((t) => ({
|
|
86
|
+
name: t.name,
|
|
87
|
+
description: t.description,
|
|
88
|
+
inputSchema: t.inputSchema,
|
|
89
|
+
}));
|
|
90
|
+
}
|
package/dist/oauth.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { CliError, EXIT } from "./errors.js";
|
|
5
|
+
/** The permissions a plain login asks for. Never includes paid generation. */
|
|
6
|
+
export const DEFAULT_SCOPE = "brand:read assets:read videos:read drafts:write";
|
|
7
|
+
/** Opt-in permission to spend credits; asked for only with `login --allow-render`. */
|
|
8
|
+
export const RENDER_SCOPE = "videos:render";
|
|
9
|
+
export function pkce() {
|
|
10
|
+
const verifier = randomBytes(32).toString("base64url");
|
|
11
|
+
const challenge = createHash("sha256").update(verifier).digest("base64url");
|
|
12
|
+
return { verifier, challenge };
|
|
13
|
+
}
|
|
14
|
+
export const randomState = () => randomBytes(16).toString("base64url");
|
|
15
|
+
/** OAuth discovery, with Personify's known paths as the fallback. */
|
|
16
|
+
export async function discover(origin, fetchImpl = fetch) {
|
|
17
|
+
const fallback = {
|
|
18
|
+
authorization_endpoint: `${origin}/dashboard/settings/mcp/authorize`,
|
|
19
|
+
token_endpoint: `${origin}/api/oauth/token`,
|
|
20
|
+
registration_endpoint: `${origin}/api/oauth/register`,
|
|
21
|
+
revocation_endpoint: `${origin}/api/oauth/revoke`,
|
|
22
|
+
resource: `${origin}/api/mcp`,
|
|
23
|
+
};
|
|
24
|
+
try {
|
|
25
|
+
const pr = await fetchImpl(`${origin}/.well-known/oauth-protected-resource/api/mcp`);
|
|
26
|
+
if (!pr.ok)
|
|
27
|
+
return fallback;
|
|
28
|
+
const resource = (await pr.json());
|
|
29
|
+
const server = resource.authorization_servers?.[0] || origin;
|
|
30
|
+
const as = await fetchImpl(`${server}/.well-known/oauth-authorization-server`);
|
|
31
|
+
if (!as.ok)
|
|
32
|
+
return { ...fallback, resource: resource.resource || fallback.resource };
|
|
33
|
+
const meta = (await as.json());
|
|
34
|
+
return {
|
|
35
|
+
authorization_endpoint: meta.authorization_endpoint || fallback.authorization_endpoint,
|
|
36
|
+
token_endpoint: meta.token_endpoint || fallback.token_endpoint,
|
|
37
|
+
registration_endpoint: meta.registration_endpoint || fallback.registration_endpoint,
|
|
38
|
+
revocation_endpoint: meta.revocation_endpoint || fallback.revocation_endpoint,
|
|
39
|
+
resource: resource.resource || fallback.resource,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return fallback;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Dynamic client registration: one public client per machine, kept in the store. */
|
|
47
|
+
export async function register(endpoints, fetchImpl = fetch) {
|
|
48
|
+
const res = await fetchImpl(endpoints.registration_endpoint, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: { "Content-Type": "application/json" },
|
|
51
|
+
body: JSON.stringify({
|
|
52
|
+
client_name: "Personify CLI",
|
|
53
|
+
redirect_uris: ["http://127.0.0.1/callback"],
|
|
54
|
+
token_endpoint_auth_method: "none",
|
|
55
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
56
|
+
response_types: ["code"],
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
const body = (await res.json().catch(() => ({})));
|
|
60
|
+
if (!res.ok || !body.client_id)
|
|
61
|
+
throw new CliError(`Could not register this CLI with Personify${body.error ? `: ${body.error}` : ""}.`, EXIT.UNAVAILABLE, "registration_failed");
|
|
62
|
+
return body.client_id;
|
|
63
|
+
}
|
|
64
|
+
export function authorizeUrl(input) {
|
|
65
|
+
const url = new URL(input.endpoints.authorization_endpoint);
|
|
66
|
+
url.search = new URLSearchParams({
|
|
67
|
+
response_type: "code",
|
|
68
|
+
client_id: input.clientId,
|
|
69
|
+
redirect_uri: input.redirectUri,
|
|
70
|
+
code_challenge: input.challenge,
|
|
71
|
+
code_challenge_method: "S256",
|
|
72
|
+
scope: input.scope,
|
|
73
|
+
state: input.state,
|
|
74
|
+
resource: input.endpoints.resource,
|
|
75
|
+
}).toString();
|
|
76
|
+
return url.href;
|
|
77
|
+
}
|
|
78
|
+
/** Read the authorization response from a pasted redirect URL. */
|
|
79
|
+
export function parseCallback(raw) {
|
|
80
|
+
let url;
|
|
81
|
+
try {
|
|
82
|
+
url = new URL(raw.trim());
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
throw new CliError("Paste the full address the browser was sent to after approving.", EXIT.USAGE, "bad_callback");
|
|
86
|
+
}
|
|
87
|
+
const error = url.searchParams.get("error");
|
|
88
|
+
if (error)
|
|
89
|
+
throw new CliError(error === "access_denied"
|
|
90
|
+
? "The connection was declined in Personify."
|
|
91
|
+
: `Personify refused the connection: ${error}.`, EXIT.AUTH, "denied");
|
|
92
|
+
const code = url.searchParams.get("code");
|
|
93
|
+
if (!code)
|
|
94
|
+
throw new CliError("No authorization code in that address.", EXIT.USAGE, "bad_callback");
|
|
95
|
+
return {
|
|
96
|
+
code,
|
|
97
|
+
state: url.searchParams.get("state") || "",
|
|
98
|
+
iss: url.searchParams.get("iss"),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** A one-shot loopback listener for the browser redirect. */
|
|
102
|
+
export function listenForCode(timeoutMs = 300_000) {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
let settle;
|
|
105
|
+
let fail;
|
|
106
|
+
const result = new Promise((res, rej) => {
|
|
107
|
+
settle = res;
|
|
108
|
+
fail = rej;
|
|
109
|
+
});
|
|
110
|
+
const server = createServer((req, res) => {
|
|
111
|
+
const url = new URL(req.url || "/", "http://127.0.0.1");
|
|
112
|
+
if (url.pathname !== "/callback") {
|
|
113
|
+
res.writeHead(404).end();
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
const callback = parseCallback(url.href);
|
|
118
|
+
res
|
|
119
|
+
.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
|
|
120
|
+
.end('<!doctype html><title>Personify CLI</title><p style="font-family:system-ui;padding:2rem">Personify CLI is connected. You can close this tab.</p>');
|
|
121
|
+
settle?.(callback);
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
res
|
|
125
|
+
.writeHead(400, { "Content-Type": "text/plain; charset=utf-8" })
|
|
126
|
+
.end("The connection was not completed. Return to the terminal.");
|
|
127
|
+
fail?.(e);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
server.once("error", reject);
|
|
131
|
+
server.listen(0, "127.0.0.1", () => {
|
|
132
|
+
const { port } = server.address();
|
|
133
|
+
const timer = setTimeout(() => fail?.(new CliError("Timed out waiting for the browser approval.", EXIT.AUTH, "timeout")), timeoutMs);
|
|
134
|
+
resolve({
|
|
135
|
+
redirectUri: `http://127.0.0.1:${port}/callback`,
|
|
136
|
+
waitForCode: () => result,
|
|
137
|
+
close: () => {
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
server.close();
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async function tokenRequest(endpoints, form, fetchImpl) {
|
|
146
|
+
const res = await fetchImpl(endpoints.token_endpoint, {
|
|
147
|
+
method: "POST",
|
|
148
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
149
|
+
body: new URLSearchParams(form).toString(),
|
|
150
|
+
});
|
|
151
|
+
const body = (await res.json().catch(() => ({})));
|
|
152
|
+
if (!res.ok || !body.access_token || !body.refresh_token)
|
|
153
|
+
throw new CliError(body.error === "invalid_grant" || res.status === 400
|
|
154
|
+
? "The login is no longer valid. Run `personify login` again."
|
|
155
|
+
: "Personify could not issue a session. Try again shortly.", res.status === 400 ? EXIT.AUTH : EXIT.UNAVAILABLE, res.status === 400 ? "invalid_grant" : "token_failed");
|
|
156
|
+
return {
|
|
157
|
+
access_token: body.access_token,
|
|
158
|
+
refresh_token: body.refresh_token,
|
|
159
|
+
expires_in: Number(body.expires_in) || 3600,
|
|
160
|
+
scope: body.scope,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export const exchange = (endpoints, input, fetchImpl = fetch) => tokenRequest(endpoints, {
|
|
164
|
+
grant_type: "authorization_code",
|
|
165
|
+
code: input.code,
|
|
166
|
+
redirect_uri: input.redirectUri,
|
|
167
|
+
client_id: input.clientId,
|
|
168
|
+
code_verifier: input.verifier,
|
|
169
|
+
resource: endpoints.resource,
|
|
170
|
+
}, fetchImpl);
|
|
171
|
+
export const refresh = (endpoints, input, fetchImpl = fetch) => tokenRequest(endpoints, {
|
|
172
|
+
grant_type: "refresh_token",
|
|
173
|
+
refresh_token: input.refreshToken,
|
|
174
|
+
client_id: input.clientId,
|
|
175
|
+
resource: endpoints.resource,
|
|
176
|
+
}, fetchImpl);
|
|
177
|
+
export async function revoke(endpoints, input, fetchImpl = fetch) {
|
|
178
|
+
if (!endpoints.revocation_endpoint)
|
|
179
|
+
return;
|
|
180
|
+
await fetchImpl(endpoints.revocation_endpoint, {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
183
|
+
body: new URLSearchParams({
|
|
184
|
+
token: input.token,
|
|
185
|
+
client_id: input.clientId,
|
|
186
|
+
}).toString(),
|
|
187
|
+
}).catch(() => undefined);
|
|
188
|
+
}
|
|
189
|
+
/** Open the system browser; false when that is not possible here. */
|
|
190
|
+
export function openBrowser(url) {
|
|
191
|
+
const [cmd, args] = process.platform === "darwin"
|
|
192
|
+
? ["open", [url]]
|
|
193
|
+
: process.platform === "win32"
|
|
194
|
+
? ["cmd", ["/c", "start", "", url]]
|
|
195
|
+
: ["xdg-open", [url]];
|
|
196
|
+
try {
|
|
197
|
+
const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
|
|
198
|
+
child.once("error", () => undefined);
|
|
199
|
+
child.unref();
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const MAX_CELL = 60;
|
|
2
|
+
const cell = (v) => {
|
|
3
|
+
if (v === null || v === undefined)
|
|
4
|
+
return "";
|
|
5
|
+
if (typeof v === "object") {
|
|
6
|
+
const s = JSON.stringify(v);
|
|
7
|
+
return s.length > MAX_CELL ? s.slice(0, MAX_CELL - 1) + "…" : s;
|
|
8
|
+
}
|
|
9
|
+
const s = String(v).replace(/\s+/g, " ");
|
|
10
|
+
return s.length > MAX_CELL ? s.slice(0, MAX_CELL - 1) + "…" : s;
|
|
11
|
+
};
|
|
12
|
+
function table(rows) {
|
|
13
|
+
if (!rows.length)
|
|
14
|
+
return "(none)";
|
|
15
|
+
const columns = [...new Set(rows.flatMap((r) => Object.keys(r)))].filter((k) => rows.some((r) => r[k] !== null && typeof r[k] !== "object"));
|
|
16
|
+
if (!columns.length)
|
|
17
|
+
return rows.map((r) => JSON.stringify(r)).join("\n");
|
|
18
|
+
const widths = columns.map((c) => Math.max(c.length, ...rows.map((r) => cell(r[c]).length)));
|
|
19
|
+
const line = (values) => values
|
|
20
|
+
.map((v, i) => v.padEnd(widths[i]))
|
|
21
|
+
.join(" ")
|
|
22
|
+
.trimEnd();
|
|
23
|
+
return [
|
|
24
|
+
line(columns),
|
|
25
|
+
line(widths.map((w) => "-".repeat(w))),
|
|
26
|
+
...rows.map((r) => line(columns.map((c) => cell(r[c])))),
|
|
27
|
+
].join("\n");
|
|
28
|
+
}
|
|
29
|
+
/** `--json` prints the data exactly; otherwise a compact human view. */
|
|
30
|
+
export function formatOutput(data, json) {
|
|
31
|
+
if (json)
|
|
32
|
+
return JSON.stringify(data, null, 2);
|
|
33
|
+
if (Array.isArray(data)) {
|
|
34
|
+
if (data.every((r) => r && typeof r === "object" && !Array.isArray(r)))
|
|
35
|
+
return table(data);
|
|
36
|
+
return data.map(cell).join("\n");
|
|
37
|
+
}
|
|
38
|
+
if (data && typeof data === "object")
|
|
39
|
+
return Object.entries(data)
|
|
40
|
+
.map(([k, v]) => Array.isArray(v) || (v && typeof v === "object")
|
|
41
|
+
? `${k}: ${JSON.stringify(v)}`
|
|
42
|
+
: `${k}: ${cell(v)}`)
|
|
43
|
+
.join("\n");
|
|
44
|
+
return cell(data);
|
|
45
|
+
}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { clearCredentials, getCredentials, setCredentials, withLock, } from "./config.js";
|
|
2
|
+
import { CliError, EXIT } from "./errors.js";
|
|
3
|
+
import { discover, refresh } from "./oauth.js";
|
|
4
|
+
const EARLY = 60_000;
|
|
5
|
+
export async function resolveSession(opts) {
|
|
6
|
+
const env = opts.env ?? process.env;
|
|
7
|
+
const now = opts.now ?? Date.now;
|
|
8
|
+
if (env.PERSONIFY_TOKEN)
|
|
9
|
+
return {
|
|
10
|
+
origin: opts.origin,
|
|
11
|
+
token: env.PERSONIFY_TOKEN,
|
|
12
|
+
scope: env.PERSONIFY_SCOPE || "",
|
|
13
|
+
source: "env",
|
|
14
|
+
};
|
|
15
|
+
const creds = getCredentials(opts.origin, env);
|
|
16
|
+
if (!creds)
|
|
17
|
+
throw new CliError(`Not signed in to ${opts.origin}. Run \`personify login\`.`, EXIT.AUTH, "unauthenticated");
|
|
18
|
+
if (creds.expires_at - now() > EARLY)
|
|
19
|
+
return {
|
|
20
|
+
origin: opts.origin,
|
|
21
|
+
token: creds.access_token,
|
|
22
|
+
scope: creds.scope,
|
|
23
|
+
expires_at: creds.expires_at,
|
|
24
|
+
source: "store",
|
|
25
|
+
};
|
|
26
|
+
return refreshSession(opts);
|
|
27
|
+
}
|
|
28
|
+
/** Rotate the session under the store lock; another process may have done it first. */
|
|
29
|
+
export async function refreshSession(opts) {
|
|
30
|
+
const env = opts.env ?? process.env;
|
|
31
|
+
const now = opts.now ?? Date.now;
|
|
32
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
33
|
+
return withLock(async () => {
|
|
34
|
+
const current = getCredentials(opts.origin, env);
|
|
35
|
+
if (!current)
|
|
36
|
+
throw new CliError(`Not signed in to ${opts.origin}. Run \`personify login\`.`, EXIT.AUTH, "unauthenticated");
|
|
37
|
+
if (current.expires_at - now() > EARLY)
|
|
38
|
+
return {
|
|
39
|
+
origin: opts.origin,
|
|
40
|
+
token: current.access_token,
|
|
41
|
+
scope: current.scope,
|
|
42
|
+
expires_at: current.expires_at,
|
|
43
|
+
source: "store",
|
|
44
|
+
};
|
|
45
|
+
const endpoints = await discover(opts.origin, fetchImpl);
|
|
46
|
+
let tokens;
|
|
47
|
+
try {
|
|
48
|
+
tokens = await refresh(endpoints, { refreshToken: current.refresh_token, clientId: current.client_id }, fetchImpl);
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
if (e instanceof CliError && e.code === "invalid_grant")
|
|
52
|
+
clearCredentials(opts.origin, env);
|
|
53
|
+
throw e;
|
|
54
|
+
}
|
|
55
|
+
const next = {
|
|
56
|
+
...current,
|
|
57
|
+
access_token: tokens.access_token,
|
|
58
|
+
refresh_token: tokens.refresh_token,
|
|
59
|
+
expires_at: now() + tokens.expires_in * 1000,
|
|
60
|
+
scope: tokens.scope || current.scope,
|
|
61
|
+
};
|
|
62
|
+
setCredentials(opts.origin, next, env);
|
|
63
|
+
return {
|
|
64
|
+
origin: opts.origin,
|
|
65
|
+
token: next.access_token,
|
|
66
|
+
scope: next.scope,
|
|
67
|
+
expires_at: next.expires_at,
|
|
68
|
+
source: "store",
|
|
69
|
+
};
|
|
70
|
+
}, env);
|
|
71
|
+
}
|
|
72
|
+
/** Run with a session; a stored session that is rejected once is refreshed and retried once. */
|
|
73
|
+
export async function withSession(opts, fn) {
|
|
74
|
+
const session = await resolveSession(opts);
|
|
75
|
+
try {
|
|
76
|
+
return await fn(session);
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
if (!(e instanceof CliError) ||
|
|
80
|
+
e.code !== "unauthenticated" ||
|
|
81
|
+
session.source !== "store")
|
|
82
|
+
throw e;
|
|
83
|
+
const env = opts.env ?? process.env;
|
|
84
|
+
const creds = getCredentials(opts.origin, env);
|
|
85
|
+
if (creds)
|
|
86
|
+
setCredentials(opts.origin, { ...creds, expires_at: 0 }, env);
|
|
87
|
+
return fn(await refreshSession(opts));
|
|
88
|
+
}
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "personify-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Command-line client for Personify (personify.gg), the AI UGC video generator for brands. Read your brand, list the 100 reel formats, create and edit video drafts, check render status and, with explicit permission, start paid renders. Built for AI agents: --json on every command, non-interactive, meaningful exit codes.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"personify": "dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json",
|
|
20
|
+
"prepack": "npm run build",
|
|
21
|
+
"test": "vitest run"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"personify",
|
|
25
|
+
"ai ugc video",
|
|
26
|
+
"ai creator videos",
|
|
27
|
+
"ugc ads",
|
|
28
|
+
"video generation",
|
|
29
|
+
"mcp",
|
|
30
|
+
"cli",
|
|
31
|
+
"agents",
|
|
32
|
+
"social media automation",
|
|
33
|
+
"brand videos"
|
|
34
|
+
],
|
|
35
|
+
"homepage": "https://personify.gg/docs/agents",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/personify-gg/personify-plugin.git"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.0.0",
|
|
42
|
+
"typescript": "^5.6.0",
|
|
43
|
+
"vitest": "^4.1.12"
|
|
44
|
+
}
|
|
45
|
+
}
|