@ptkl/toolkit 0.11.0 → 1.0.0
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/README.md +101 -0
- package/dist/bin/toolkit.js +0 -0
- package/dist/commands/billing.js +111 -0
- package/dist/commands/config.js +33 -0
- package/dist/commands/forge.js +731 -36
- package/dist/commands/functions.js +87 -22
- package/dist/commands/index.js +25 -1
- package/dist/commands/integration.js +146 -0
- package/dist/commands/integrations/dms.js +625 -0
- package/dist/commands/integrations/mail.js +120 -0
- package/dist/commands/integrations/payments.js +199 -0
- package/dist/commands/integrations/serbia.js +610 -0
- package/dist/commands/integrations/timber.js +120 -0
- package/dist/commands/kortex.js +297 -0
- package/dist/commands/logs.js +96 -0
- package/dist/commands/project.js +147 -0
- package/dist/commands/ratchet.js +82 -0
- package/dist/commands/sandbox.js +122 -0
- package/dist/commands/satellite.js +111 -0
- package/dist/commands/template.js +96 -0
- package/dist/commands/thunder.js +123 -0
- package/dist/commands/users.js +31 -19
- package/dist/commands/workflow.js +225 -30
- package/dist/lib/forgeDevKernelPlugin.js +2 -1
- package/dist/lib/kortex/bridge.js +113 -0
- package/dist/lib/kortex/config.js +21 -0
- package/dist/lib/kortex/conversations.js +84 -0
- package/dist/lib/kortex/mcp.js +193 -0
- package/dist/lib/kortex/session.js +763 -0
- package/dist/lib/kortex/tools.js +406 -0
- package/dist/lib/kortex/ui/App.js +455 -0
- package/dist/lib/kortex/ui/SelectAgent.js +23 -0
- package/dist/lib/scaffold.js +51 -0
- package/dist/lib/sdk.js +88 -0
- package/package.json +11 -2
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import * as sdk from "../../lib/sdk.js";
|
|
3
|
+
import { registerCommand, withProfile } from "../../lib/scaffold.js";
|
|
4
|
+
import { integrationSub, isActive } from "../integration.js";
|
|
5
|
+
/** `ptkl integration timber` — typed interface for the Protokol Timber (audit log) integration
|
|
6
|
+
* (RFC §4.3/§6.1 layer 2). Maps 1:1 onto the SDK `Timber` client (`@ptkl/sdk/beta`, same door as
|
|
7
|
+
* `Integrations.getTimber()`): `query`/`write`/`usage`. Every subcommand precheck's
|
|
8
|
+
* `isActive('protokol-timber')` so an inactive integration surfaces the friendly "run `ptkl
|
|
9
|
+
* integration activate protokol-timber`" hint instead of a raw 403.
|
|
10
|
+
*
|
|
11
|
+
* The top-level `logs` command (src/commands/logs.ts, pre-existing back-compat surface) delegates
|
|
12
|
+
* its data fetch to `query()` below rather than duplicating the SDK call — see logs.ts. */
|
|
13
|
+
const TIMBER_ID = "protokol-timber";
|
|
14
|
+
/** Build the Timber SDK client for `profile`, re-attaching the `X-Project-Uuid` header the
|
|
15
|
+
* pre-existing back-compat `logs` command deliberately set (`client.client.defaults.headers
|
|
16
|
+
* .common['X-Project-Uuid'] = util.getCurrentProfile().project`) before every query/write/usage
|
|
17
|
+
* call. `sdk.integrationsOptions()` passes `project` through, but `IntegrationsBaseClient`
|
|
18
|
+
* (worktrees/sdk/src/v0.10/api/integrationsBaseClient.ts) only ever sets `X-Project-Uuid` from
|
|
19
|
+
* `process.env.PROJECT_UUID`, which the CLI never populates — so without setting it here, every
|
|
20
|
+
* request in this file would silently drop project scoping and rely solely on the token being
|
|
21
|
+
* project-scoped, a behavioral regression vs. the original `logs` command. */
|
|
22
|
+
function timberClient(profile) {
|
|
23
|
+
const client = sdk.timber(profile);
|
|
24
|
+
client.client.defaults.headers.common['X-Project-Uuid'] = profile.project;
|
|
25
|
+
return client;
|
|
26
|
+
}
|
|
27
|
+
function parseJSON(label, raw) {
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(raw);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
console.error(`${label} must be valid JSON`);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Shared implementation behind `ptkl integration timber query` — also used directly by the
|
|
37
|
+
* top-level `logs` alias (src/commands/logs.ts) so the two commands never drift. */
|
|
38
|
+
export async function query(profile, opts) {
|
|
39
|
+
await isActive(profile, TIMBER_ID);
|
|
40
|
+
return await timberClient(profile).query({
|
|
41
|
+
limit: opts.limit,
|
|
42
|
+
...(opts.cursor ? { cursor: opts.cursor } : {}),
|
|
43
|
+
...(opts.source ? { source: opts.source } : {}),
|
|
44
|
+
...(opts.level ? { level: opts.level } : {}),
|
|
45
|
+
...(opts.from ? { from: opts.from } : {}),
|
|
46
|
+
...(opts.to ? { to: opts.to } : {}),
|
|
47
|
+
...(opts.actorId ? { actor_id: opts.actorId } : {}),
|
|
48
|
+
...(opts.actorName ? { actor_name: opts.actorName } : {}),
|
|
49
|
+
...(opts.actorType ? { actor_type: opts.actorType } : {}),
|
|
50
|
+
...(opts.action ? { action: opts.action } : {}),
|
|
51
|
+
...(opts.resourceType ? { resource_type: opts.resourceType } : {}),
|
|
52
|
+
...(opts.resourceId ? { resource_id: opts.resourceId } : {}),
|
|
53
|
+
...(opts.requestUuid ? { request_uuid: opts.requestUuid } : {}),
|
|
54
|
+
...(opts.search ? { search: opts.search } : {}),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
class TimberCommand {
|
|
58
|
+
register() {
|
|
59
|
+
const cmd = new Command("timber").description("Protokol Timber integration: query/write audit-log entries, view usage");
|
|
60
|
+
cmd.addCommand(this.registerQuery());
|
|
61
|
+
cmd.addCommand(this.registerWrite());
|
|
62
|
+
cmd.addCommand(this.registerUsage());
|
|
63
|
+
integrationSub(cmd);
|
|
64
|
+
return cmd;
|
|
65
|
+
}
|
|
66
|
+
registerQuery() {
|
|
67
|
+
return registerCommand(new Command("query")
|
|
68
|
+
.description("Query Timber audit-log entries for the current project")
|
|
69
|
+
.option("--source <source>", "Filter by source: system | user")
|
|
70
|
+
.option("--level <level>", "Filter by level: debug | info | warn | error")
|
|
71
|
+
.option("--from <datetime>", "Start of time range (ISO 8601)")
|
|
72
|
+
.option("--to <datetime>", "End of time range (ISO 8601)")
|
|
73
|
+
.option("--actor-id <id>", "Filter by actor id")
|
|
74
|
+
.option("--actor-name <name>", "Filter by actor name")
|
|
75
|
+
.option("--actor-type <type>", "Filter by actor type: user | api_key | service | system")
|
|
76
|
+
.option("--action <action>", "Filter by action")
|
|
77
|
+
.option("--resource-type <type>", "Filter by resource type")
|
|
78
|
+
.option("--resource-id <id>", "Filter by resource id")
|
|
79
|
+
.option("--request-uuid <uuid>", "Filter by request uuid")
|
|
80
|
+
.option("-s, --search <text>", "Search message, actor, or resource")
|
|
81
|
+
.option("-l, --limit <limit>", "Number of logs to fetch", (v) => parseInt(v, 10), 50)
|
|
82
|
+
.option("-c, --cursor <cursor>", "Pagination cursor from a previous response")).action(withProfile(async (profile, opts) => await query(profile, opts)));
|
|
83
|
+
}
|
|
84
|
+
registerWrite() {
|
|
85
|
+
return registerCommand(new Command("write")
|
|
86
|
+
.description("Write an entry to the Timber audit log")
|
|
87
|
+
.requiredOption("-m, --message <message>", "Log message")
|
|
88
|
+
.option("--level <level>", "Log level: debug | info | warn | error", "info")
|
|
89
|
+
.option("--service <service>", "Originating service name")
|
|
90
|
+
.option("--resource-type <type>", "Related resource type")
|
|
91
|
+
.option("--resource-id <id>", "Related resource id")
|
|
92
|
+
.option("--resource-name <name>", "Related resource name")
|
|
93
|
+
.option("-a, --attributes <json>", "Extra attributes as a JSON object string")
|
|
94
|
+
.option("--trace-id <id>", "Distributed trace id")
|
|
95
|
+
.option("--span-id <id>", "Distributed span id")
|
|
96
|
+
.option("--request-id <id>", "Request id")).action(withProfile(async (profile, opts) => {
|
|
97
|
+
await isActive(profile, TIMBER_ID);
|
|
98
|
+
const attributes = opts.attributes ? parseJSON("--attributes", opts.attributes) : undefined;
|
|
99
|
+
return await timberClient(profile).write({
|
|
100
|
+
message: opts.message,
|
|
101
|
+
level: opts.level,
|
|
102
|
+
...(opts.service ? { service: opts.service } : {}),
|
|
103
|
+
...(opts.resourceType ? { resource_type: opts.resourceType } : {}),
|
|
104
|
+
...(opts.resourceId ? { resource_id: opts.resourceId } : {}),
|
|
105
|
+
...(opts.resourceName ? { resource_name: opts.resourceName } : {}),
|
|
106
|
+
...(attributes !== undefined ? { attributes } : {}),
|
|
107
|
+
...(opts.traceId ? { trace_id: opts.traceId } : {}),
|
|
108
|
+
...(opts.spanId ? { span_id: opts.spanId } : {}),
|
|
109
|
+
...(opts.requestId ? { request_id: opts.requestId } : {}),
|
|
110
|
+
});
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
registerUsage() {
|
|
114
|
+
return registerCommand(new Command("usage").description("Show Timber ingestion usage for the current project")).action(withProfile(async (profile) => {
|
|
115
|
+
await isActive(profile, TIMBER_ID);
|
|
116
|
+
return await timberClient(profile).usage();
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export default new TimberCommand();
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { basename } from "path";
|
|
4
|
+
import util from "../lib/util.js";
|
|
5
|
+
import { kortex as kortexClient } from "../lib/sdk.js";
|
|
6
|
+
import { registerCommand, withProfile } from "../lib/scaffold.js";
|
|
7
|
+
import { UIBridge } from "../lib/kortex/bridge.js";
|
|
8
|
+
import Session from "../lib/kortex/session.js";
|
|
9
|
+
import { loadKortexConfig } from "../lib/kortex/config.js";
|
|
10
|
+
const PLATFORM_AGENT_NAMES = {
|
|
11
|
+
'protokol-bot': 'ProtokolBot',
|
|
12
|
+
'proka': 'Proka',
|
|
13
|
+
'prota': 'Prota',
|
|
14
|
+
};
|
|
15
|
+
function parseJSON(label, raw) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(raw);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
console.error(`${label} must be valid JSON`);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class KortexCommand {
|
|
25
|
+
register() {
|
|
26
|
+
const cmd = new Command('kortex')
|
|
27
|
+
.description('Interactive AI coding assistant powered by Kortex (bare invocation launches the TUI)')
|
|
28
|
+
.argument('[prompt...]', 'Optional initial prompt')
|
|
29
|
+
// RESERVED: `-a`/`--agent`, `-c`/`--conversation`, `--cloud` are parent options.
|
|
30
|
+
// Commander resolves options against the whole parsed command chain, so a
|
|
31
|
+
// parent-owned flag anywhere in argv (including after a subcommand name) is claimed
|
|
32
|
+
// by the parent and never reaches the subcommand — the subcommand's own
|
|
33
|
+
// (possibly `requiredOption`) declaration of the same name is unreachable. No
|
|
34
|
+
// subcommand under `kortex` may declare `-a`, `-c`, `--agent`, `--conversation`, or
|
|
35
|
+
// `--cloud`; use different long names (e.g. `--agent-uuid`) instead.
|
|
36
|
+
.option('-a, --agent <uuid>', 'Agent UUID to use')
|
|
37
|
+
.option('-c, --conversation <uuid>', 'Resume a conversation (local or cloud)')
|
|
38
|
+
.option('--cloud', 'Save conversation on the platform (default: local only)')
|
|
39
|
+
.action(this.run);
|
|
40
|
+
// Non-interactive management subtree — RFC §6 `kortex` tree (agent/kb/doc/conversation
|
|
41
|
+
// + usage/tier/top-up), all via the SDK `Kortex` client (src/lib/sdk.ts). The bare
|
|
42
|
+
// `kortex [prompt...]` action above (and its TUI/MCP bridge) is untouched.
|
|
43
|
+
cmd.addCommand(this.registerAgent());
|
|
44
|
+
cmd.addCommand(this.registerKB());
|
|
45
|
+
cmd.addCommand(this.registerDoc());
|
|
46
|
+
cmd.addCommand(this.registerConversation());
|
|
47
|
+
cmd.addCommand(registerCommand(new Command('usage')
|
|
48
|
+
.description('Get aggregated Kortex usage for the current project (admin), or a single user with --user')
|
|
49
|
+
.option('-u, --user <uuid>', 'User-access UUID to scope usage to')
|
|
50
|
+
.option('-m, --month <yyyy-mm>', 'Month to scope usage to')
|
|
51
|
+
.option('--breakdown', 'With --user, return the monthly usage breakdown instead of the current month')
|
|
52
|
+
.option('-p, --page <page>', 'Page number')
|
|
53
|
+
.option('-l, --limit <limit>', 'Page size')).action(withProfile(async (profile, opts) => {
|
|
54
|
+
const client = kortexClient(profile);
|
|
55
|
+
if (opts.user) {
|
|
56
|
+
if (opts.breakdown) {
|
|
57
|
+
return (await client.getUserUsageBreakdown(opts.user)).data;
|
|
58
|
+
}
|
|
59
|
+
return (await client.getUserMonthlyUsage(opts.user, { month: opts.month })).data;
|
|
60
|
+
}
|
|
61
|
+
const params = {
|
|
62
|
+
month: opts.month,
|
|
63
|
+
page: opts.page ? Number(opts.page) : undefined,
|
|
64
|
+
limit: opts.limit ? Number(opts.limit) : undefined,
|
|
65
|
+
};
|
|
66
|
+
return (await client.listUsage(params)).data;
|
|
67
|
+
})));
|
|
68
|
+
cmd.addCommand(registerCommand(new Command('tier').description('Get the current project\'s Kortex tier')).action(withProfile(async (profile) => {
|
|
69
|
+
return (await kortexClient(profile).getTier()).data;
|
|
70
|
+
})));
|
|
71
|
+
cmd.addCommand(registerCommand(new Command('top-up')
|
|
72
|
+
.description('Top up credits for a Kortex user-access record')
|
|
73
|
+
.requiredOption('-u, --uuid <uuid>', 'User-access UUID to top up')
|
|
74
|
+
.requiredOption('--amount <amount>', 'Amount of credits to add')
|
|
75
|
+
.option('-r, --reason <reason>', 'Reason for the top-up')).action(withProfile(async (profile, opts) => {
|
|
76
|
+
// No dedicated `topUp` method exists on the SDK `Kortex` client yet — reuse its
|
|
77
|
+
// authenticated axios instance (same pattern as project.ts's billing-modes/auto-topup)
|
|
78
|
+
// instead of hand-rolling a raw axios call.
|
|
79
|
+
const client = kortexClient(profile);
|
|
80
|
+
const res = await client.client.post(`/luma/kortex/v1/user-access/${opts.uuid}/top-up`, {
|
|
81
|
+
amount: Number(opts.amount),
|
|
82
|
+
reason: opts.reason,
|
|
83
|
+
});
|
|
84
|
+
return res.data;
|
|
85
|
+
})));
|
|
86
|
+
return cmd;
|
|
87
|
+
}
|
|
88
|
+
registerAgent() {
|
|
89
|
+
const agent = new Command('agent').description('Manage Kortex agents');
|
|
90
|
+
agent.addCommand(registerCommand(new Command('list')
|
|
91
|
+
.description('List Kortex agents')
|
|
92
|
+
.option('-p, --page <page>', 'Page number')
|
|
93
|
+
.option('-l, --limit <limit>', 'Page size')).action(withProfile(async (profile, opts) => {
|
|
94
|
+
const params = { page: opts.page ? Number(opts.page) : undefined, limit: opts.limit ? Number(opts.limit) : undefined };
|
|
95
|
+
return (await kortexClient(profile).listAgents(params)).data;
|
|
96
|
+
})));
|
|
97
|
+
agent.addCommand(registerCommand(new Command('get').description('Get a Kortex agent by UUID').requiredOption('-u, --uuid <uuid>', 'Agent UUID')).action(withProfile(async (profile, opts) => {
|
|
98
|
+
return (await kortexClient(profile).getAgent(opts.uuid)).data;
|
|
99
|
+
})));
|
|
100
|
+
agent.addCommand(registerCommand(new Command('create')
|
|
101
|
+
.description('Create a Kortex agent')
|
|
102
|
+
.requiredOption('-d, --data <json>', 'Agent payload as a JSON string (name, description, system_prompt, knowledge_bases, rag_config, llm_settings, model_tier)')).action(withProfile(async (profile, opts) => {
|
|
103
|
+
const data = parseJSON('--data', opts.data);
|
|
104
|
+
return (await kortexClient(profile).createAgent(data)).data;
|
|
105
|
+
})));
|
|
106
|
+
agent.addCommand(registerCommand(new Command('update')
|
|
107
|
+
.description('Update a Kortex agent')
|
|
108
|
+
.requiredOption('-u, --uuid <uuid>', 'Agent UUID')
|
|
109
|
+
.requiredOption('-d, --data <json>', 'Fields to update as a JSON string')).action(withProfile(async (profile, opts) => {
|
|
110
|
+
const data = parseJSON('--data', opts.data);
|
|
111
|
+
return (await kortexClient(profile).updateAgent(opts.uuid, data)).data;
|
|
112
|
+
})));
|
|
113
|
+
agent.addCommand(registerCommand(new Command('delete').description('Delete a Kortex agent').requiredOption('-u, --uuid <uuid>', 'Agent UUID')).action(withProfile(async (profile, opts) => {
|
|
114
|
+
return (await kortexClient(profile).deleteAgent(opts.uuid)).data;
|
|
115
|
+
})));
|
|
116
|
+
return agent;
|
|
117
|
+
}
|
|
118
|
+
registerKB() {
|
|
119
|
+
const kb = new Command('kb').description('Manage Kortex knowledge bases');
|
|
120
|
+
kb.addCommand(registerCommand(new Command('list')
|
|
121
|
+
.description('List Kortex knowledge bases')
|
|
122
|
+
.option('-p, --page <page>', 'Page number')
|
|
123
|
+
.option('-l, --limit <limit>', 'Page size')).action(withProfile(async (profile, opts) => {
|
|
124
|
+
const params = { page: opts.page ? Number(opts.page) : undefined, limit: opts.limit ? Number(opts.limit) : undefined };
|
|
125
|
+
return (await kortexClient(profile).listKnowledgeBases(params)).data;
|
|
126
|
+
})));
|
|
127
|
+
kb.addCommand(registerCommand(new Command('create')
|
|
128
|
+
.description('Create a Kortex knowledge base')
|
|
129
|
+
.requiredOption('-d, --data <json>', 'Knowledge base payload as a JSON string (name, description, chunking_strategy)')).action(withProfile(async (profile, opts) => {
|
|
130
|
+
const data = parseJSON('--data', opts.data);
|
|
131
|
+
return (await kortexClient(profile).createKnowledgeBase(data)).data;
|
|
132
|
+
})));
|
|
133
|
+
kb.addCommand(registerCommand(new Command('update')
|
|
134
|
+
.description('Update a Kortex knowledge base')
|
|
135
|
+
.requiredOption('-u, --uuid <uuid>', 'Knowledge base UUID')
|
|
136
|
+
.requiredOption('-d, --data <json>', 'Fields to update as a JSON string')).action(withProfile(async (profile, opts) => {
|
|
137
|
+
const data = parseJSON('--data', opts.data);
|
|
138
|
+
return (await kortexClient(profile).updateKnowledgeBase(opts.uuid, data)).data;
|
|
139
|
+
})));
|
|
140
|
+
kb.addCommand(registerCommand(new Command('delete').description('Delete a Kortex knowledge base').requiredOption('-u, --uuid <uuid>', 'Knowledge base UUID')).action(withProfile(async (profile, opts) => {
|
|
141
|
+
return (await kortexClient(profile).deleteKnowledgeBase(opts.uuid)).data;
|
|
142
|
+
})));
|
|
143
|
+
kb.addCommand(registerCommand(new Command('search')
|
|
144
|
+
.description('RAG search over the knowledge bases attached to an agent')
|
|
145
|
+
.argument('<query>', 'Search query')
|
|
146
|
+
// NOTE: cannot use `-a`/`--agent` here — the parent `kortex` command owns those
|
|
147
|
+
// (reserved TUI flags, see the comment on register()'s `cmd` options above);
|
|
148
|
+
// commander resolves parent-owned option names before the subcommand ever sees
|
|
149
|
+
// them, regardless of where in argv they appear.
|
|
150
|
+
.requiredOption('--agent-uuid <uuid>', 'Agent UUID (knowledge bases are derived from the agent)')
|
|
151
|
+
.option('-k, --top-k <n>', 'Number of chunks to return (server-clamped 1-10)')).action(withProfile(async (profile, query, opts) => {
|
|
152
|
+
const payload = { agent_uuid: opts.agentUuid, query, top_k: opts.topK ? Number(opts.topK) : undefined };
|
|
153
|
+
return (await kortexClient(profile).searchKnowledgeBase(payload)).data;
|
|
154
|
+
})));
|
|
155
|
+
return kb;
|
|
156
|
+
}
|
|
157
|
+
registerDoc() {
|
|
158
|
+
const doc = new Command('doc').description('Manage documents within a Kortex knowledge base');
|
|
159
|
+
doc.addCommand(registerCommand(new Command('list')
|
|
160
|
+
.description('List documents in a knowledge base')
|
|
161
|
+
.requiredOption('-k, --kb <uuid>', 'Knowledge base UUID')
|
|
162
|
+
.option('-p, --page <page>', 'Page number')
|
|
163
|
+
.option('-l, --limit <limit>', 'Page size')).action(withProfile(async (profile, opts) => {
|
|
164
|
+
const params = { page: opts.page ? Number(opts.page) : undefined, limit: opts.limit ? Number(opts.limit) : undefined };
|
|
165
|
+
return (await kortexClient(profile).listDocuments(opts.kb, params)).data;
|
|
166
|
+
})));
|
|
167
|
+
doc.addCommand(registerCommand(new Command('upload')
|
|
168
|
+
.description('Upload a document to a knowledge base')
|
|
169
|
+
.requiredOption('-k, --kb <uuid>', 'Knowledge base UUID')
|
|
170
|
+
.requiredOption('--file <path>', 'Path to the file to upload')
|
|
171
|
+
.option('-n, --name <name>', 'Override the uploaded file name (default: the file\'s basename)')).action(withProfile(async (profile, opts) => {
|
|
172
|
+
const buffer = readFileSync(opts.file);
|
|
173
|
+
const formData = new FormData();
|
|
174
|
+
formData.append('file', new Blob([buffer]), opts.name ?? basename(opts.file));
|
|
175
|
+
return (await kortexClient(profile).uploadDocument(opts.kb, formData)).data;
|
|
176
|
+
})));
|
|
177
|
+
doc.addCommand(registerCommand(new Command('get')
|
|
178
|
+
.description('Get a document by UUID')
|
|
179
|
+
.requiredOption('-k, --kb <uuid>', 'Knowledge base UUID')
|
|
180
|
+
.requiredOption('-u, --uuid <uuid>', 'Document UUID')).action(withProfile(async (profile, opts) => {
|
|
181
|
+
return (await kortexClient(profile).getDocument(opts.kb, opts.uuid)).data;
|
|
182
|
+
})));
|
|
183
|
+
doc.addCommand(registerCommand(new Command('delete')
|
|
184
|
+
.description('Delete a document')
|
|
185
|
+
.requiredOption('-k, --kb <uuid>', 'Knowledge base UUID')
|
|
186
|
+
.requiredOption('-u, --uuid <uuid>', 'Document UUID')).action(withProfile(async (profile, opts) => {
|
|
187
|
+
return (await kortexClient(profile).deleteDocument(opts.kb, opts.uuid)).data;
|
|
188
|
+
})));
|
|
189
|
+
doc.addCommand(registerCommand(new Command('retry')
|
|
190
|
+
.description('Retry processing a failed document')
|
|
191
|
+
.requiredOption('-k, --kb <uuid>', 'Knowledge base UUID')
|
|
192
|
+
.requiredOption('-u, --uuid <uuid>', 'Document UUID')).action(withProfile(async (profile, opts) => {
|
|
193
|
+
return (await kortexClient(profile).retryDocument(opts.kb, opts.uuid)).data;
|
|
194
|
+
})));
|
|
195
|
+
return doc;
|
|
196
|
+
}
|
|
197
|
+
registerConversation() {
|
|
198
|
+
const conversation = new Command('conversation').description('Manage Kortex conversations');
|
|
199
|
+
conversation.addCommand(registerCommand(new Command('list')
|
|
200
|
+
.description('List Kortex conversations')
|
|
201
|
+
.option('--all-users', 'Include conversations from all users (admin)')
|
|
202
|
+
.option('--archived', 'Include archived conversations')
|
|
203
|
+
.option('-p, --page <page>', 'Page number')
|
|
204
|
+
.option('-l, --limit <limit>', 'Page size')).action(withProfile(async (profile, opts) => {
|
|
205
|
+
const params = {
|
|
206
|
+
page: opts.page ? Number(opts.page) : undefined,
|
|
207
|
+
limit: opts.limit ? Number(opts.limit) : undefined,
|
|
208
|
+
all_users: opts.allUsers ? true : undefined,
|
|
209
|
+
archived: opts.archived ? true : undefined,
|
|
210
|
+
};
|
|
211
|
+
return (await kortexClient(profile).listConversations(params)).data;
|
|
212
|
+
})));
|
|
213
|
+
conversation.addCommand(registerCommand(new Command('get').description('Get a Kortex conversation by UUID').requiredOption('-u, --uuid <uuid>', 'Conversation UUID')).action(withProfile(async (profile, opts) => {
|
|
214
|
+
return (await kortexClient(profile).getConversation(opts.uuid)).data;
|
|
215
|
+
})));
|
|
216
|
+
conversation.addCommand(registerCommand(new Command('delete').description('Delete a Kortex conversation').requiredOption('-u, --uuid <uuid>', 'Conversation UUID')).action(withProfile(async (profile, opts) => {
|
|
217
|
+
return (await kortexClient(profile).deleteConversation(opts.uuid)).data;
|
|
218
|
+
})));
|
|
219
|
+
return conversation;
|
|
220
|
+
}
|
|
221
|
+
async run(promptParts, opts) {
|
|
222
|
+
let profile;
|
|
223
|
+
try {
|
|
224
|
+
profile = util.getCurrentProfile();
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
let profiles = [];
|
|
228
|
+
try {
|
|
229
|
+
profiles = util.getProfiles() ?? [];
|
|
230
|
+
}
|
|
231
|
+
catch { /* no profiles file */ }
|
|
232
|
+
if (profiles.length === 0) {
|
|
233
|
+
console.error('No profiles found. Run `ptkl init` and then `ptkl profile new` to create one.');
|
|
234
|
+
process.exit(1);
|
|
235
|
+
}
|
|
236
|
+
console.error('No active profile. Available profiles:');
|
|
237
|
+
for (const p of profiles) {
|
|
238
|
+
console.error(` ${p.name}`);
|
|
239
|
+
}
|
|
240
|
+
console.error(`\nRun \`ptkl profile use <name>\` or use \`ptkl --profile <name> kortex\``);
|
|
241
|
+
process.exit(1);
|
|
242
|
+
}
|
|
243
|
+
// Resolve agent: CLI flag → persisted last used → ProtokolBot
|
|
244
|
+
const config = loadKortexConfig();
|
|
245
|
+
const agentUUID = opts.agent ?? config.lastAgentUUID ?? 'protokol-bot';
|
|
246
|
+
let agentName = PLATFORM_AGENT_NAMES[agentUUID] ?? 'Kortex';
|
|
247
|
+
if (!PLATFORM_AGENT_NAMES[agentUUID]) {
|
|
248
|
+
try {
|
|
249
|
+
const resp = await kortexClient(profile).getAgent(agentUUID);
|
|
250
|
+
agentName = resp.data?.name ?? agentName;
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
// Unknown agent, fall back to ProtokolBot
|
|
254
|
+
agentName = 'ProtokolBot';
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const isLocal = !opts.cloud;
|
|
258
|
+
const initialPrompt = promptParts.length > 0 ? promptParts.join(' ') : undefined;
|
|
259
|
+
const bridge = new UIBridge({
|
|
260
|
+
agentUUID,
|
|
261
|
+
agentName,
|
|
262
|
+
cwd: process.cwd(),
|
|
263
|
+
isLocal,
|
|
264
|
+
profileName: profile.name,
|
|
265
|
+
});
|
|
266
|
+
// Force color support before marked-terminal's bundled chalk initializes
|
|
267
|
+
process.env.FORCE_COLOR = '3';
|
|
268
|
+
// Enter alternate screen buffer (fullscreen takeover)
|
|
269
|
+
process.stdout.write('\x1b[?1049h\x1b[2J\x1b[H');
|
|
270
|
+
const exitAltScreen = () => { process.stdout.write('\x1b[?1049l'); };
|
|
271
|
+
process.once('SIGTERM', () => { exitAltScreen(); process.exit(0); });
|
|
272
|
+
const [{ render }, React, { App }] = await Promise.all([
|
|
273
|
+
import('ink'),
|
|
274
|
+
import('react'),
|
|
275
|
+
import('../lib/kortex/ui/App.js'),
|
|
276
|
+
]);
|
|
277
|
+
const { waitUntilExit } = render(React.createElement(App, { bridge }));
|
|
278
|
+
const session = new Session({
|
|
279
|
+
token: profile.token,
|
|
280
|
+
host: profile.host,
|
|
281
|
+
agentUUID,
|
|
282
|
+
agentName,
|
|
283
|
+
cwd: process.cwd(),
|
|
284
|
+
conversationUUID: opts.conversation,
|
|
285
|
+
initialPrompt,
|
|
286
|
+
cloud: !!opts.cloud,
|
|
287
|
+
bridge,
|
|
288
|
+
});
|
|
289
|
+
session.start().catch((err) => {
|
|
290
|
+
bridge.showError(err?.message || String(err));
|
|
291
|
+
bridge.quit();
|
|
292
|
+
});
|
|
293
|
+
await waitUntilExit();
|
|
294
|
+
exitAltScreen();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
export default new KortexCommand();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { query as timberQuery } from "./integrations/timber.js";
|
|
3
|
+
import util from "../lib/util.js";
|
|
4
|
+
class LogsCommand {
|
|
5
|
+
register() {
|
|
6
|
+
return new Command("logs")
|
|
7
|
+
.description("Query or tail Timber logs for the current project")
|
|
8
|
+
.option("--source <source>", "Filter by source: system | user")
|
|
9
|
+
.option("--level <level>", "Filter by level: debug | info | warn | error")
|
|
10
|
+
.option("--action <action>", "Filter by action")
|
|
11
|
+
.option("--resource-type <type>", "Filter by resource type")
|
|
12
|
+
.option("-s, --search <text>", "Search message, actor, or resource")
|
|
13
|
+
.option("-l, --limit <limit>", "Number of logs to fetch", "50")
|
|
14
|
+
.option("-t, --tail", "Poll Timber and print new logs")
|
|
15
|
+
.option("--interval <seconds>", "Tail polling interval", "3")
|
|
16
|
+
.option("--json", "Print newline-delimited JSON")
|
|
17
|
+
.action(this.run);
|
|
18
|
+
}
|
|
19
|
+
async run(opts) {
|
|
20
|
+
const profile = util.getCurrentProfile();
|
|
21
|
+
const seen = new Set();
|
|
22
|
+
const fetch = async () => {
|
|
23
|
+
const result = await timberQuery(profile, LogsCommand.params(opts));
|
|
24
|
+
return result.data || [];
|
|
25
|
+
};
|
|
26
|
+
const print = (events) => {
|
|
27
|
+
events.reverse().forEach((event) => {
|
|
28
|
+
const key = event.id || `${event.timestamp}:${event.service}:${event.resource_id}:${event.message}`;
|
|
29
|
+
if (seen.has(key)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
seen.add(key);
|
|
33
|
+
if (opts.json) {
|
|
34
|
+
console.log(JSON.stringify(event));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
console.log(LogsCommand.format(event));
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
print(await fetch());
|
|
41
|
+
if (!opts.tail) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const interval = Math.max(1, Number(opts.interval || 3)) * 1000;
|
|
45
|
+
setInterval(async () => {
|
|
46
|
+
try {
|
|
47
|
+
print(await fetch());
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
console.error(`Timber tail failed: ${err.message}`);
|
|
51
|
+
}
|
|
52
|
+
}, interval);
|
|
53
|
+
}
|
|
54
|
+
static params(opts) {
|
|
55
|
+
const params = {
|
|
56
|
+
limit: Number(opts.limit || 50),
|
|
57
|
+
};
|
|
58
|
+
if (opts.source)
|
|
59
|
+
params.source = opts.source;
|
|
60
|
+
if (opts.level)
|
|
61
|
+
params.level = opts.level;
|
|
62
|
+
if (opts.action)
|
|
63
|
+
params.action = opts.action;
|
|
64
|
+
if (opts.resourceType)
|
|
65
|
+
params.resourceType = opts.resourceType;
|
|
66
|
+
if (opts.search)
|
|
67
|
+
params.search = opts.search;
|
|
68
|
+
return params;
|
|
69
|
+
}
|
|
70
|
+
static format(event) {
|
|
71
|
+
const time = event.timestamp ? new Date(event.timestamp).toISOString() : "-";
|
|
72
|
+
const level = (event.level || "info").toUpperCase().padEnd(5);
|
|
73
|
+
const service = (event.service || "-").padEnd(12);
|
|
74
|
+
const action = event.action || "-";
|
|
75
|
+
const resource = event.resource_name || event.resource_id || event.resource_type || "-";
|
|
76
|
+
const message = event.message || `${action} ${resource}`;
|
|
77
|
+
const actor = event.actor
|
|
78
|
+
? event.actor.name || event.actor.id || "-"
|
|
79
|
+
: "-";
|
|
80
|
+
let line = `${time} ${level} ${service} ${message}`;
|
|
81
|
+
if (action !== "-")
|
|
82
|
+
line += ` action=${action}`;
|
|
83
|
+
if (resource !== "-")
|
|
84
|
+
line += ` resource=${resource}`;
|
|
85
|
+
if (actor !== "-")
|
|
86
|
+
line += ` actor=${actor}`;
|
|
87
|
+
if (event.attributes && Object.keys(event.attributes).length) {
|
|
88
|
+
const attrs = Object.entries(event.attributes)
|
|
89
|
+
.map(([k, v]) => `${k}=${typeof v === "object" ? JSON.stringify(v) : v}`)
|
|
90
|
+
.join(" ");
|
|
91
|
+
line += ` ${attrs}`;
|
|
92
|
+
}
|
|
93
|
+
return line;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export default new LogsCommand();
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import * as sdk from "../lib/sdk.js";
|
|
3
|
+
import { registerCommand, withProfile } from "../lib/scaffold.js";
|
|
4
|
+
/** `ptkl project` — project/workspace/invite/user control-plane (RFC §6, "Project control-plane").
|
|
5
|
+
* Maps 1:1 onto the SDK `Project` client (@ptkl/sdk/beta) except `billing-modes` and `auto-topup`,
|
|
6
|
+
* which the platform exposes (platform-api/internal/project/controllers/{billing_modes,edit_auto_topup_settings}.go)
|
|
7
|
+
* but the v0.10 `Project` client does not yet wrap in typed methods. Those two subcommands go
|
|
8
|
+
* through the same authenticated client instance's underlying `.client` (a public AxiosInstance,
|
|
9
|
+
* see @ptkl/sdk/beta BaseClient) rather than a named method — a stopgap for a follow-up SDK WP to
|
|
10
|
+
* give `Project` `getBillingModes()/updateBillingModes()/updateAutoTopupSettings()` methods. */
|
|
11
|
+
function parseJSON(label, raw) {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(raw);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
console.error(`${label} must be valid JSON`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class ProjectCommand {
|
|
21
|
+
register() {
|
|
22
|
+
const cmd = new Command("project").description("Manage projects, workspaces, invites and project users");
|
|
23
|
+
cmd.addCommand(registerCommand(new Command("create")
|
|
24
|
+
.description("Create a new project")
|
|
25
|
+
.requiredOption("-n, --name <name>", "Project name")
|
|
26
|
+
.option("-d, --description <description>", "Project description")).action(withProfile(async (profile, opts) => {
|
|
27
|
+
const { name, description } = opts;
|
|
28
|
+
const res = await sdk.project(profile).create({ name, description });
|
|
29
|
+
return res.data;
|
|
30
|
+
})));
|
|
31
|
+
cmd.addCommand(registerCommand(new Command("list").description("List all projects for the current user")).action(withProfile(async (profile) => {
|
|
32
|
+
const res = await sdk.project(profile).list();
|
|
33
|
+
return res.data;
|
|
34
|
+
})));
|
|
35
|
+
cmd.addCommand(registerCommand(new Command("info").description("Get information about the current project")).action(withProfile(async (profile) => {
|
|
36
|
+
const res = await sdk.project(profile).info();
|
|
37
|
+
return res.data;
|
|
38
|
+
})));
|
|
39
|
+
cmd.addCommand(registerCommand(new Command("archive").description("Archive the current project")).action(withProfile(async (profile) => {
|
|
40
|
+
const res = await sdk.project(profile).archive();
|
|
41
|
+
return res.data;
|
|
42
|
+
})));
|
|
43
|
+
cmd.addCommand(registerCommand(new Command("settings")
|
|
44
|
+
.description("Update project settings")
|
|
45
|
+
.requiredOption("-d, --data <json>", "Settings object as a JSON string")).action(withProfile(async (profile, opts) => {
|
|
46
|
+
const settings = parseJSON("--data", opts.data);
|
|
47
|
+
const res = await sdk.project(profile).updateSettings(settings);
|
|
48
|
+
return res.data;
|
|
49
|
+
})));
|
|
50
|
+
cmd.addCommand(this.registerWorkspace());
|
|
51
|
+
cmd.addCommand(registerCommand(new Command("invite")
|
|
52
|
+
.description("Invite users to the current project")
|
|
53
|
+
.requiredOption("-e, --emails <emails...>", "Email addresses to invite")
|
|
54
|
+
.requiredOption("-r, --roles <roles...>", "Role UUIDs to grant the invitees")).action(withProfile(async (profile, opts) => {
|
|
55
|
+
const { emails, roles } = opts;
|
|
56
|
+
const res = await sdk.project(profile).invite(emails, roles);
|
|
57
|
+
return res.data;
|
|
58
|
+
})));
|
|
59
|
+
cmd.addCommand(registerCommand(new Command("invites").description("List pending invites for the current project")).action(withProfile(async (profile) => {
|
|
60
|
+
const res = await sdk.project(profile).getInvites();
|
|
61
|
+
return res.data;
|
|
62
|
+
})));
|
|
63
|
+
cmd.addCommand(registerCommand(new Command("accept")
|
|
64
|
+
.description("Accept a project invite")
|
|
65
|
+
.requiredOption("-u, --uuid <uuid>", "Invite UUID")).action(withProfile(async (profile, opts) => {
|
|
66
|
+
const res = await sdk.project(profile).acceptInvite(opts.uuid);
|
|
67
|
+
return res.data;
|
|
68
|
+
})));
|
|
69
|
+
cmd.addCommand(this.registerUsers());
|
|
70
|
+
cmd.addCommand(registerCommand(new Command("billing-modes")
|
|
71
|
+
.description("Get or update the project's per-service usage billing mode overrides")
|
|
72
|
+
.option("-s, --set <json>", "Usage billing modes to set, as a JSON object string (e.g. '{\"kortex\":\"credits\"}'); omit to read the current modes")).action(withProfile(async (profile, opts) => {
|
|
73
|
+
const client = sdk.project(profile);
|
|
74
|
+
if (opts.set) {
|
|
75
|
+
const usage_billing_modes = parseJSON("--set", opts.set);
|
|
76
|
+
const res = await client.client.put("/v1/project/billing-modes", { usage_billing_modes });
|
|
77
|
+
return res.data;
|
|
78
|
+
}
|
|
79
|
+
const res = await client.client.get("/v1/project/billing-modes");
|
|
80
|
+
return res.data;
|
|
81
|
+
})));
|
|
82
|
+
cmd.addCommand(registerCommand(new Command("auto-topup")
|
|
83
|
+
.description("Configure automatic balance top-up for the current project")
|
|
84
|
+
.requiredOption("--enabled <enabled>", "Enable or disable auto top-up: true | false")
|
|
85
|
+
.option("--threshold <threshold>", "Balance threshold (in cents) that triggers a top-up", "0")
|
|
86
|
+
.option("--amount <amount>", "Amount (in cents) to top up when triggered", "0")).action(withProfile(async (profile, opts) => {
|
|
87
|
+
const client = sdk.project(profile);
|
|
88
|
+
const body = {
|
|
89
|
+
auto_topup_enabled: opts.enabled === "true",
|
|
90
|
+
auto_topup_threshold: Number(opts.threshold),
|
|
91
|
+
auto_topup_amount: Number(opts.amount),
|
|
92
|
+
};
|
|
93
|
+
const res = await client.client.put("/v1/project/auto-topup-settings", body);
|
|
94
|
+
return res.data;
|
|
95
|
+
})));
|
|
96
|
+
return cmd;
|
|
97
|
+
}
|
|
98
|
+
registerWorkspace() {
|
|
99
|
+
const workspace = new Command("workspace").description("Manage project workspaces");
|
|
100
|
+
workspace.addCommand(registerCommand(new Command("create")
|
|
101
|
+
.description("Create a new workspace")
|
|
102
|
+
.requiredOption("-n, --name <name>", "Workspace name")
|
|
103
|
+
.option("-d, --description <description>", "Workspace description")).action(withProfile(async (profile, opts) => {
|
|
104
|
+
const { name, description } = opts;
|
|
105
|
+
const res = await sdk.project(profile).createWorkspace({ name, description });
|
|
106
|
+
return res.data;
|
|
107
|
+
})));
|
|
108
|
+
workspace.addCommand(registerCommand(new Command("update")
|
|
109
|
+
.description("Update a workspace")
|
|
110
|
+
.requiredOption("-u, --uuid <uuid>", "Workspace UUID")
|
|
111
|
+
.requiredOption("-d, --data <json>", "Fields to update, as a JSON object string")).action(withProfile(async (profile, opts) => {
|
|
112
|
+
const data = parseJSON("--data", opts.data);
|
|
113
|
+
const res = await sdk.project(profile).updateWorkspace(opts.uuid, data);
|
|
114
|
+
return res.data;
|
|
115
|
+
})));
|
|
116
|
+
workspace.addCommand(registerCommand(new Command("delete")
|
|
117
|
+
.description("Delete a workspace")
|
|
118
|
+
.requiredOption("-u, --uuid <uuid>", "Workspace UUID")).action(withProfile(async (profile, opts) => {
|
|
119
|
+
const res = await sdk.project(profile).deleteWorkspace(opts.uuid);
|
|
120
|
+
return res.data;
|
|
121
|
+
})));
|
|
122
|
+
return workspace;
|
|
123
|
+
}
|
|
124
|
+
registerUsers() {
|
|
125
|
+
const users = new Command("users").description("Manage users in the current project");
|
|
126
|
+
users.addCommand(registerCommand(new Command("list").description("List users in the current project")).action(withProfile(async (profile) => {
|
|
127
|
+
const res = await sdk.project(profile).getUsers();
|
|
128
|
+
return res.data;
|
|
129
|
+
})));
|
|
130
|
+
users.addCommand(registerCommand(new Command("update")
|
|
131
|
+
.description("Update a project user")
|
|
132
|
+
.requiredOption("-u, --uuid <uuid>", "User UUID")
|
|
133
|
+
.requiredOption("-d, --data <json>", "Fields to update, as a JSON object string")).action(withProfile(async (profile, opts) => {
|
|
134
|
+
const data = parseJSON("--data", opts.data);
|
|
135
|
+
const res = await sdk.project(profile).updateUser(opts.uuid, data);
|
|
136
|
+
return res.data;
|
|
137
|
+
})));
|
|
138
|
+
users.addCommand(registerCommand(new Command("remove")
|
|
139
|
+
.description("Remove a user from the project")
|
|
140
|
+
.requiredOption("-u, --uuid <uuid>", "User UUID")).action(withProfile(async (profile, opts) => {
|
|
141
|
+
const res = await sdk.project(profile).deleteUser(opts.uuid);
|
|
142
|
+
return res.data;
|
|
143
|
+
})));
|
|
144
|
+
return users;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export default new ProjectCommand();
|