@geminixiang/mikan 0.2.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/CHANGELOG.md +324 -0
- package/LICENSE +22 -0
- package/README.md +297 -0
- package/dist/adapter.d.ts +134 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +2 -0
- package/dist/adapter.js.map +1 -0
- package/dist/adapters/discord/bot.d.ts +63 -0
- package/dist/adapters/discord/bot.d.ts.map +1 -0
- package/dist/adapters/discord/bot.js +577 -0
- package/dist/adapters/discord/bot.js.map +1 -0
- package/dist/adapters/discord/context.d.ts +9 -0
- package/dist/adapters/discord/context.d.ts.map +1 -0
- package/dist/adapters/discord/context.js +245 -0
- package/dist/adapters/discord/context.js.map +1 -0
- package/dist/adapters/discord/index.d.ts +3 -0
- package/dist/adapters/discord/index.d.ts.map +1 -0
- package/dist/adapters/discord/index.js +3 -0
- package/dist/adapters/discord/index.js.map +1 -0
- package/dist/adapters/shared.d.ts +91 -0
- package/dist/adapters/shared.d.ts.map +1 -0
- package/dist/adapters/shared.js +191 -0
- package/dist/adapters/shared.js.map +1 -0
- package/dist/adapters/slack/bot.d.ts +139 -0
- package/dist/adapters/slack/bot.d.ts.map +1 -0
- package/dist/adapters/slack/bot.js +1272 -0
- package/dist/adapters/slack/bot.js.map +1 -0
- package/dist/adapters/slack/branch-manager.d.ts +28 -0
- package/dist/adapters/slack/branch-manager.d.ts.map +1 -0
- package/dist/adapters/slack/branch-manager.js +117 -0
- package/dist/adapters/slack/branch-manager.js.map +1 -0
- package/dist/adapters/slack/context.d.ts +12 -0
- package/dist/adapters/slack/context.d.ts.map +1 -0
- package/dist/adapters/slack/context.js +327 -0
- package/dist/adapters/slack/context.js.map +1 -0
- package/dist/adapters/slack/index.d.ts +3 -0
- package/dist/adapters/slack/index.d.ts.map +1 -0
- package/dist/adapters/slack/index.js +3 -0
- package/dist/adapters/slack/index.js.map +1 -0
- package/dist/adapters/slack/session.d.ts +38 -0
- package/dist/adapters/slack/session.d.ts.map +1 -0
- package/dist/adapters/slack/session.js +66 -0
- package/dist/adapters/slack/session.js.map +1 -0
- package/dist/adapters/slack/tools/attach.d.ts +12 -0
- package/dist/adapters/slack/tools/attach.d.ts.map +1 -0
- package/dist/adapters/slack/tools/attach.js +40 -0
- package/dist/adapters/slack/tools/attach.js.map +1 -0
- package/dist/adapters/telegram/bot.d.ts +51 -0
- package/dist/adapters/telegram/bot.d.ts.map +1 -0
- package/dist/adapters/telegram/bot.js +430 -0
- package/dist/adapters/telegram/bot.js.map +1 -0
- package/dist/adapters/telegram/context.d.ts +9 -0
- package/dist/adapters/telegram/context.d.ts.map +1 -0
- package/dist/adapters/telegram/context.js +190 -0
- package/dist/adapters/telegram/context.js.map +1 -0
- package/dist/adapters/telegram/html.d.ts +3 -0
- package/dist/adapters/telegram/html.d.ts.map +1 -0
- package/dist/adapters/telegram/html.js +98 -0
- package/dist/adapters/telegram/html.js.map +1 -0
- package/dist/adapters/telegram/index.d.ts +3 -0
- package/dist/adapters/telegram/index.d.ts.map +1 -0
- package/dist/adapters/telegram/index.js +3 -0
- package/dist/adapters/telegram/index.js.map +1 -0
- package/dist/agent.d.ts +36 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +1147 -0
- package/dist/agent.js.map +1 -0
- package/dist/commands/auto-reply.d.ts +5 -0
- package/dist/commands/auto-reply.d.ts.map +1 -0
- package/dist/commands/auto-reply.js +79 -0
- package/dist/commands/auto-reply.js.map +1 -0
- package/dist/commands/index.d.ts +5 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +18 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/login.d.ts +5 -0
- package/dist/commands/login.d.ts.map +1 -0
- package/dist/commands/login.js +91 -0
- package/dist/commands/login.js.map +1 -0
- package/dist/commands/model.d.ts +14 -0
- package/dist/commands/model.d.ts.map +1 -0
- package/dist/commands/model.js +110 -0
- package/dist/commands/model.js.map +1 -0
- package/dist/commands/new.d.ts +5 -0
- package/dist/commands/new.d.ts.map +1 -0
- package/dist/commands/new.js +24 -0
- package/dist/commands/new.js.map +1 -0
- package/dist/commands/parse.d.ts +7 -0
- package/dist/commands/parse.d.ts.map +1 -0
- package/dist/commands/parse.js +17 -0
- package/dist/commands/parse.js.map +1 -0
- package/dist/commands/registry.d.ts +4 -0
- package/dist/commands/registry.d.ts.map +1 -0
- package/dist/commands/registry.js +9 -0
- package/dist/commands/registry.js.map +1 -0
- package/dist/commands/sandbox.d.ts +10 -0
- package/dist/commands/sandbox.d.ts.map +1 -0
- package/dist/commands/sandbox.js +83 -0
- package/dist/commands/sandbox.js.map +1 -0
- package/dist/commands/session-view.d.ts +5 -0
- package/dist/commands/session-view.d.ts.map +1 -0
- package/dist/commands/session-view.js +62 -0
- package/dist/commands/session-view.js.map +1 -0
- package/dist/commands/types.d.ts +41 -0
- package/dist/commands/types.d.ts.map +1 -0
- package/dist/commands/types.js +2 -0
- package/dist/commands/types.js.map +1 -0
- package/dist/commands/utils.d.ts +8 -0
- package/dist/commands/utils.d.ts.map +1 -0
- package/dist/commands/utils.js +14 -0
- package/dist/commands/utils.js.map +1 -0
- package/dist/config.d.ts +59 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +370 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +17 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +24 -0
- package/dist/context.js.map +1 -0
- package/dist/conversation-history.d.ts +16 -0
- package/dist/conversation-history.d.ts.map +1 -0
- package/dist/conversation-history.js +144 -0
- package/dist/conversation-history.js.map +1 -0
- package/dist/download.d.ts +2 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +89 -0
- package/dist/download.js.map +1 -0
- package/dist/env.d.ts +3 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +12 -0
- package/dist/env.js.map +1 -0
- package/dist/events.d.ts +85 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +483 -0
- package/dist/events.js.map +1 -0
- package/dist/execution-resolver.d.ts +25 -0
- package/dist/execution-resolver.d.ts.map +1 -0
- package/dist/execution-resolver.js +167 -0
- package/dist/execution-resolver.js.map +1 -0
- package/dist/file-guards.d.ts +9 -0
- package/dist/file-guards.d.ts.map +1 -0
- package/dist/file-guards.js +56 -0
- package/dist/file-guards.js.map +1 -0
- package/dist/fs-atomic.d.ts +10 -0
- package/dist/fs-atomic.d.ts.map +1 -0
- package/dist/fs-atomic.js +45 -0
- package/dist/fs-atomic.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/instrument.d.ts +2 -0
- package/dist/instrument.d.ts.map +1 -0
- package/dist/instrument.js +10 -0
- package/dist/instrument.js.map +1 -0
- package/dist/log.d.ts +36 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +206 -0
- package/dist/log.js.map +1 -0
- package/dist/login/index.d.ts +42 -0
- package/dist/login/index.d.ts.map +1 -0
- package/dist/login/index.js +239 -0
- package/dist/login/index.js.map +1 -0
- package/dist/login/portal.d.ts +19 -0
- package/dist/login/portal.d.ts.map +1 -0
- package/dist/login/portal.js +1544 -0
- package/dist/login/portal.js.map +1 -0
- package/dist/login/session.d.ts +26 -0
- package/dist/login/session.d.ts.map +1 -0
- package/dist/login/session.js +56 -0
- package/dist/login/session.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +366 -0
- package/dist/main.js.map +1 -0
- package/dist/provisioner.d.ts +83 -0
- package/dist/provisioner.d.ts.map +1 -0
- package/dist/provisioner.js +500 -0
- package/dist/provisioner.js.map +1 -0
- package/dist/runtime/conversation-orchestrator.d.ts +40 -0
- package/dist/runtime/conversation-orchestrator.d.ts.map +1 -0
- package/dist/runtime/conversation-orchestrator.js +183 -0
- package/dist/runtime/conversation-orchestrator.js.map +1 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +2 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/session-runtime.d.ts +26 -0
- package/dist/runtime/session-runtime.d.ts.map +1 -0
- package/dist/runtime/session-runtime.js +221 -0
- package/dist/runtime/session-runtime.js.map +1 -0
- package/dist/sandbox/cloudflare.d.ts +15 -0
- package/dist/sandbox/cloudflare.d.ts.map +1 -0
- package/dist/sandbox/cloudflare.js +138 -0
- package/dist/sandbox/cloudflare.js.map +1 -0
- package/dist/sandbox/container.d.ts +16 -0
- package/dist/sandbox/container.d.ts.map +1 -0
- package/dist/sandbox/container.js +138 -0
- package/dist/sandbox/container.js.map +1 -0
- package/dist/sandbox/errors.d.ts +6 -0
- package/dist/sandbox/errors.d.ts.map +1 -0
- package/dist/sandbox/errors.js +11 -0
- package/dist/sandbox/errors.js.map +1 -0
- package/dist/sandbox/firecracker.d.ts +17 -0
- package/dist/sandbox/firecracker.d.ts.map +1 -0
- package/dist/sandbox/firecracker.js +212 -0
- package/dist/sandbox/firecracker.js.map +1 -0
- package/dist/sandbox/host.d.ts +11 -0
- package/dist/sandbox/host.d.ts.map +1 -0
- package/dist/sandbox/host.js +89 -0
- package/dist/sandbox/host.js.map +1 -0
- package/dist/sandbox/image.d.ts +5 -0
- package/dist/sandbox/image.d.ts.map +1 -0
- package/dist/sandbox/image.js +30 -0
- package/dist/sandbox/image.js.map +1 -0
- package/dist/sandbox/index.d.ts +22 -0
- package/dist/sandbox/index.d.ts.map +1 -0
- package/dist/sandbox/index.js +54 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/path-context.d.ts +4 -0
- package/dist/sandbox/path-context.d.ts.map +1 -0
- package/dist/sandbox/path-context.js +20 -0
- package/dist/sandbox/path-context.js.map +1 -0
- package/dist/sandbox/types.d.ts +67 -0
- package/dist/sandbox/types.d.ts.map +1 -0
- package/dist/sandbox/types.js +2 -0
- package/dist/sandbox/types.js.map +1 -0
- package/dist/sandbox/utils.d.ts +4 -0
- package/dist/sandbox/utils.d.ts.map +1 -0
- package/dist/sandbox/utils.js +51 -0
- package/dist/sandbox/utils.js.map +1 -0
- package/dist/sentry.d.ts +50 -0
- package/dist/sentry.d.ts.map +1 -0
- package/dist/sentry.js +257 -0
- package/dist/sentry.js.map +1 -0
- package/dist/session-view/command.d.ts +5 -0
- package/dist/session-view/command.d.ts.map +1 -0
- package/dist/session-view/command.js +7 -0
- package/dist/session-view/command.js.map +1 -0
- package/dist/session-view/portal.d.ts +16 -0
- package/dist/session-view/portal.d.ts.map +1 -0
- package/dist/session-view/portal.js +1822 -0
- package/dist/session-view/portal.js.map +1 -0
- package/dist/session-view/service.d.ts +34 -0
- package/dist/session-view/service.d.ts.map +1 -0
- package/dist/session-view/service.js +434 -0
- package/dist/session-view/service.js.map +1 -0
- package/dist/session-view/store.d.ts +18 -0
- package/dist/session-view/store.d.ts.map +1 -0
- package/dist/session-view/store.js +36 -0
- package/dist/session-view/store.js.map +1 -0
- package/dist/sessions/metadata.d.ts +15 -0
- package/dist/sessions/metadata.d.ts.map +1 -0
- package/dist/sessions/metadata.js +11 -0
- package/dist/sessions/metadata.js.map +1 -0
- package/dist/sessions/policy.d.ts +13 -0
- package/dist/sessions/policy.d.ts.map +1 -0
- package/dist/sessions/policy.js +23 -0
- package/dist/sessions/policy.js.map +1 -0
- package/dist/sessions/store.d.ts +103 -0
- package/dist/sessions/store.d.ts.map +1 -0
- package/dist/sessions/store.js +349 -0
- package/dist/sessions/store.js.map +1 -0
- package/dist/store.d.ts +58 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +152 -0
- package/dist/store.js.map +1 -0
- package/dist/tool-diagnostics.d.ts +2 -0
- package/dist/tool-diagnostics.d.ts.map +1 -0
- package/dist/tool-diagnostics.js +7 -0
- package/dist/tool-diagnostics.js.map +1 -0
- package/dist/tools/bash.d.ts +10 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +80 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/edit.d.ts +11 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +133 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/event.d.ts +62 -0
- package/dist/tools/event.d.ts.map +1 -0
- package/dist/tools/event.js +138 -0
- package/dist/tools/event.js.map +1 -0
- package/dist/tools/index.d.ts +14 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +23 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/read.d.ts +11 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +136 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/truncate.d.ts +57 -0
- package/dist/tools/truncate.d.ts.map +1 -0
- package/dist/tools/truncate.js +184 -0
- package/dist/tools/truncate.js.map +1 -0
- package/dist/tools/write.d.ts +10 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +33 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/trigger.d.ts +31 -0
- package/dist/trigger.d.ts.map +1 -0
- package/dist/trigger.js +98 -0
- package/dist/trigger.js.map +1 -0
- package/dist/ui-copy.d.ts +12 -0
- package/dist/ui-copy.d.ts.map +1 -0
- package/dist/ui-copy.js +36 -0
- package/dist/ui-copy.js.map +1 -0
- package/dist/vault-routing.d.ts +4 -0
- package/dist/vault-routing.d.ts.map +1 -0
- package/dist/vault-routing.js +16 -0
- package/dist/vault-routing.js.map +1 -0
- package/dist/vault.d.ts +72 -0
- package/dist/vault.d.ts.map +1 -0
- package/dist/vault.js +281 -0
- package/dist/vault.js.map +1 -0
- package/package.json +83 -0
package/dist/log.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
function timestamp() {
|
|
3
|
+
const now = new Date();
|
|
4
|
+
const hh = String(now.getHours()).padStart(2, "0");
|
|
5
|
+
const mm = String(now.getMinutes()).padStart(2, "0");
|
|
6
|
+
const ss = String(now.getSeconds()).padStart(2, "0");
|
|
7
|
+
return `[${hh}:${mm}:${ss}]`;
|
|
8
|
+
}
|
|
9
|
+
function formatContext(ctx) {
|
|
10
|
+
const session = ctx.sessionId ? `:${ctx.sessionId}` : "";
|
|
11
|
+
if (ctx.conversationId.startsWith("D")) {
|
|
12
|
+
return `[DM:${ctx.userName || ctx.conversationId}${session}]`;
|
|
13
|
+
}
|
|
14
|
+
const conversation = ctx.conversationName || ctx.conversationId;
|
|
15
|
+
const user = ctx.userName || "unknown";
|
|
16
|
+
return `[${conversation.startsWith("#") ? conversation : `#${conversation}`}:${user}${session}]`;
|
|
17
|
+
}
|
|
18
|
+
// Keep stdout/stderr lines manageable when echoing tool/agent output. Long bodies
|
|
19
|
+
// flow through Sentry and session storage; the console stream just needs a preview.
|
|
20
|
+
const LOG_PREVIEW_MAX = 1000;
|
|
21
|
+
function truncate(text, maxLen) {
|
|
22
|
+
if (text.length <= maxLen)
|
|
23
|
+
return text;
|
|
24
|
+
return `${text.substring(0, maxLen)}\n(truncated at ${maxLen} chars)`;
|
|
25
|
+
}
|
|
26
|
+
function formatToolArgs(args) {
|
|
27
|
+
const lines = [];
|
|
28
|
+
for (const [key, value] of Object.entries(args)) {
|
|
29
|
+
// Skip the label - it's already shown in the tool name
|
|
30
|
+
if (key === "label")
|
|
31
|
+
continue;
|
|
32
|
+
// For read tool, format path with offset/limit
|
|
33
|
+
if (key === "path" && typeof value === "string") {
|
|
34
|
+
const offset = args.offset;
|
|
35
|
+
const limit = args.limit;
|
|
36
|
+
if (offset !== undefined && limit !== undefined) {
|
|
37
|
+
lines.push(`${value}:${offset}-${offset + limit}`);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
lines.push(value);
|
|
41
|
+
}
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// Skip offset/limit since we already handled them
|
|
45
|
+
if (key === "offset" || key === "limit")
|
|
46
|
+
continue;
|
|
47
|
+
// For other values, format them
|
|
48
|
+
if (typeof value === "string") {
|
|
49
|
+
// Multi-line strings get indented
|
|
50
|
+
if (value.includes("\n")) {
|
|
51
|
+
lines.push(value);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
lines.push(value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
lines.push(JSON.stringify(value));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return lines.join("\n");
|
|
62
|
+
}
|
|
63
|
+
// User messages
|
|
64
|
+
export function logUserMessage(ctx, text) {
|
|
65
|
+
console.log(chalk.green(`${timestamp()} ${formatContext(ctx)} ${text}`));
|
|
66
|
+
}
|
|
67
|
+
// Tool execution
|
|
68
|
+
export function logToolStart(ctx, toolName, label, args) {
|
|
69
|
+
const formattedArgs = formatToolArgs(args);
|
|
70
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ↳ ${toolName}: ${label}`));
|
|
71
|
+
if (formattedArgs) {
|
|
72
|
+
// Indent the args
|
|
73
|
+
const indented = formattedArgs
|
|
74
|
+
.split("\n")
|
|
75
|
+
.map((line) => ` ${line}`)
|
|
76
|
+
.join("\n");
|
|
77
|
+
console.log(chalk.dim(indented));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export function logToolSuccess(ctx, toolName, durationMs, result) {
|
|
81
|
+
const duration = (durationMs / 1000).toFixed(1);
|
|
82
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✓ ${toolName} (${duration}s)`));
|
|
83
|
+
const truncated = truncate(result, LOG_PREVIEW_MAX);
|
|
84
|
+
if (truncated) {
|
|
85
|
+
const indented = truncated
|
|
86
|
+
.split("\n")
|
|
87
|
+
.map((line) => ` ${line}`)
|
|
88
|
+
.join("\n");
|
|
89
|
+
console.log(chalk.dim(indented));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export function logToolError(ctx, toolName, durationMs, error) {
|
|
93
|
+
const duration = (durationMs / 1000).toFixed(1);
|
|
94
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✗ ${toolName} (${duration}s)`));
|
|
95
|
+
const truncated = truncate(error, LOG_PREVIEW_MAX);
|
|
96
|
+
const indented = truncated
|
|
97
|
+
.split("\n")
|
|
98
|
+
.map((line) => ` ${line}`)
|
|
99
|
+
.join("\n");
|
|
100
|
+
console.log(chalk.dim(indented));
|
|
101
|
+
}
|
|
102
|
+
// Response streaming
|
|
103
|
+
export function logResponseStart(ctx) {
|
|
104
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} → Streaming response...`));
|
|
105
|
+
}
|
|
106
|
+
export function logThinking(ctx, thinking) {
|
|
107
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💭 Thinking`));
|
|
108
|
+
const truncated = truncate(thinking, LOG_PREVIEW_MAX);
|
|
109
|
+
const indented = truncated
|
|
110
|
+
.split("\n")
|
|
111
|
+
.map((line) => ` ${line}`)
|
|
112
|
+
.join("\n");
|
|
113
|
+
console.log(chalk.dim(indented));
|
|
114
|
+
}
|
|
115
|
+
export function logResponse(ctx, text) {
|
|
116
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💬 Response`));
|
|
117
|
+
const truncated = truncate(text, LOG_PREVIEW_MAX);
|
|
118
|
+
const indented = truncated
|
|
119
|
+
.split("\n")
|
|
120
|
+
.map((line) => ` ${line}`)
|
|
121
|
+
.join("\n");
|
|
122
|
+
console.log(chalk.dim(indented));
|
|
123
|
+
}
|
|
124
|
+
// System
|
|
125
|
+
export function logInfo(message) {
|
|
126
|
+
console.log(chalk.blue(`${timestamp()} [system] ${message}`));
|
|
127
|
+
}
|
|
128
|
+
export function logWarning(message, details) {
|
|
129
|
+
console.log(chalk.yellow(`${timestamp()} [system] ⚠ ${message}`));
|
|
130
|
+
if (details) {
|
|
131
|
+
const indented = details
|
|
132
|
+
.split("\n")
|
|
133
|
+
.map((line) => ` ${line}`)
|
|
134
|
+
.join("\n");
|
|
135
|
+
console.log(chalk.dim(indented));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export function logAgentError(ctx, error) {
|
|
139
|
+
const context = ctx === "system" ? "[system]" : formatContext(ctx);
|
|
140
|
+
console.log(chalk.yellow(`${timestamp()} ${context} ✗ Agent error`));
|
|
141
|
+
const indented = error
|
|
142
|
+
.split("\n")
|
|
143
|
+
.map((line) => ` ${line}`)
|
|
144
|
+
.join("\n");
|
|
145
|
+
console.log(chalk.dim(indented));
|
|
146
|
+
}
|
|
147
|
+
function formatTokenCount(count) {
|
|
148
|
+
if (count < 1000)
|
|
149
|
+
return count.toString();
|
|
150
|
+
if (count < 10000)
|
|
151
|
+
return `${(count / 1000).toFixed(1)}k`;
|
|
152
|
+
if (count < 1000000)
|
|
153
|
+
return `${Math.round(count / 1000)}k`;
|
|
154
|
+
return `${(count / 1000000).toFixed(1)}M`;
|
|
155
|
+
}
|
|
156
|
+
// Usage summary
|
|
157
|
+
export function logUsageSummary(ctx, usage, contextTokens, contextWindow) {
|
|
158
|
+
const lines = [];
|
|
159
|
+
lines.push("_Usage Summary_");
|
|
160
|
+
lines.push(`Tokens: ${usage.input.toLocaleString()} in, ${usage.output.toLocaleString()} out`);
|
|
161
|
+
if (usage.cacheRead > 0 || usage.cacheWrite > 0) {
|
|
162
|
+
lines.push(`Cache: ${usage.cacheRead.toLocaleString()} read, ${usage.cacheWrite.toLocaleString()} write`);
|
|
163
|
+
}
|
|
164
|
+
if (contextTokens && contextWindow) {
|
|
165
|
+
const contextPercent = ((contextTokens / contextWindow) * 100).toFixed(1);
|
|
166
|
+
lines.push(`Context: ${formatTokenCount(contextTokens)} / ${formatTokenCount(contextWindow)} (${contextPercent}%)`);
|
|
167
|
+
}
|
|
168
|
+
lines.push(`Cost: $${usage.cost.input.toFixed(4)} in, $${usage.cost.output.toFixed(4)} out` +
|
|
169
|
+
(usage.cacheRead > 0 || usage.cacheWrite > 0
|
|
170
|
+
? `, $${usage.cost.cacheRead.toFixed(4)} cache read, $${usage.cost.cacheWrite.toFixed(4)} cache write`
|
|
171
|
+
: ""));
|
|
172
|
+
lines.push(`*Total: $${usage.cost.total.toFixed(4)}*`);
|
|
173
|
+
const summary = lines.join("\n");
|
|
174
|
+
console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💰 Usage`));
|
|
175
|
+
console.log(chalk.dim(` ${usage.input.toLocaleString()} in + ${usage.output.toLocaleString()} out` +
|
|
176
|
+
(usage.cacheRead > 0 || usage.cacheWrite > 0
|
|
177
|
+
? ` (${usage.cacheRead.toLocaleString()} cache read, ${usage.cacheWrite.toLocaleString()} cache write)`
|
|
178
|
+
: "") +
|
|
179
|
+
` = $${usage.cost.total.toFixed(4)}`));
|
|
180
|
+
return summary;
|
|
181
|
+
}
|
|
182
|
+
// Startup (no context needed)
|
|
183
|
+
export function logStartup(workingDir, sandbox) {
|
|
184
|
+
console.log("Starting mikan...");
|
|
185
|
+
console.log(` Working directory: ${workingDir}`);
|
|
186
|
+
console.log(` Sandbox: ${sandbox}`);
|
|
187
|
+
}
|
|
188
|
+
export function logConnected(platform) {
|
|
189
|
+
console.log(`⚡️ Mikan connected to ${platform} and listening!`);
|
|
190
|
+
console.log("");
|
|
191
|
+
}
|
|
192
|
+
export function logDisconnected() {
|
|
193
|
+
console.log("Mikan disconnected.");
|
|
194
|
+
}
|
|
195
|
+
// Backfill
|
|
196
|
+
export function logBackfillStart(channelCount) {
|
|
197
|
+
console.log(chalk.blue(`${timestamp()} [system] Backfilling ${channelCount} channels...`));
|
|
198
|
+
}
|
|
199
|
+
export function logBackfillChannel(channelName, messageCount) {
|
|
200
|
+
console.log(chalk.blue(`${timestamp()} [system] #${channelName}: ${messageCount} messages`));
|
|
201
|
+
}
|
|
202
|
+
export function logBackfillComplete(totalMessages, durationMs) {
|
|
203
|
+
const duration = (durationMs / 1000).toFixed(1);
|
|
204
|
+
console.log(chalk.blue(`${timestamp()} [system] Backfill complete: ${totalMessages} messages in ${duration}s`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,8BAA8B;AAC9B,MAAM,UAAU,UAAU,CAAC,UAAkB,EAAE,OAAe;IAC5D,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACjC,OAAO,CAAC,GAAG,CAAC,wBAAwB,UAAU,EAAE,CAAC,CAAC;IAClD,OAAO,CAAC,GAAG,CAAC,cAAc,OAAO,EAAE,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,QAAgB;IAC3C,OAAO,CAAC,GAAG,CAAC,yBAAyB,QAAQ,iBAAiB,CAAC,CAAC;IAChE,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAClB,CAAC;AAED,MAAM,UAAU,eAAe;IAC7B,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AACrC,CAAC;AAED,WAAW;AACX,MAAM,UAAU,gBAAgB,CAAC,YAAoB;IACnD,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,SAAS,EAAE,yBAAyB,YAAY,cAAc,CAAC,CAAC,CAAC;AAC7F,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,WAAmB,EAAE,YAAoB;IAC1E,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,SAAS,EAAE,gBAAgB,WAAW,KAAK,YAAY,WAAW,CAAC,CAAC,CAAC;AACjG,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,aAAqB,EAAE,UAAkB;IAC3E,MAAM,QAAQ,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,GAAG,CACT,KAAK,CAAC,IAAI,CACR,GAAG,SAAS,EAAE,gCAAgC,aAAa,gBAAgB,QAAQ,GAAG,CACvF,CACF,CAAC;AACJ,CAAC","sourcesContent":["import chalk from \"chalk\";\n\nexport interface LogContext {\n conversationId: string;\n userName?: string;\n conversationName?: string; // For display like #dev-team vs C16HET4EQ\n sessionId?: string;\n}\n\nfunction timestamp(): string {\n const now = new Date();\n const hh = String(now.getHours()).padStart(2, \"0\");\n const mm = String(now.getMinutes()).padStart(2, \"0\");\n const ss = String(now.getSeconds()).padStart(2, \"0\");\n return `[${hh}:${mm}:${ss}]`;\n}\n\nfunction formatContext(ctx: LogContext): string {\n const session = ctx.sessionId ? `:${ctx.sessionId}` : \"\";\n if (ctx.conversationId.startsWith(\"D\")) {\n return `[DM:${ctx.userName || ctx.conversationId}${session}]`;\n }\n const conversation = ctx.conversationName || ctx.conversationId;\n const user = ctx.userName || \"unknown\";\n return `[${conversation.startsWith(\"#\") ? conversation : `#${conversation}`}:${user}${session}]`;\n}\n\n// Keep stdout/stderr lines manageable when echoing tool/agent output. Long bodies\n// flow through Sentry and session storage; the console stream just needs a preview.\nconst LOG_PREVIEW_MAX = 1000;\n\nfunction truncate(text: string, maxLen: number): string {\n if (text.length <= maxLen) return text;\n return `${text.substring(0, maxLen)}\\n(truncated at ${maxLen} chars)`;\n}\n\nfunction formatToolArgs(args: Record<string, unknown>): string {\n const lines: string[] = [];\n\n for (const [key, value] of Object.entries(args)) {\n // Skip the label - it's already shown in the tool name\n if (key === \"label\") continue;\n\n // For read tool, format path with offset/limit\n if (key === \"path\" && typeof value === \"string\") {\n const offset = args.offset as number | undefined;\n const limit = args.limit as number | undefined;\n if (offset !== undefined && limit !== undefined) {\n lines.push(`${value}:${offset}-${offset + limit}`);\n } else {\n lines.push(value);\n }\n continue;\n }\n\n // Skip offset/limit since we already handled them\n if (key === \"offset\" || key === \"limit\") continue;\n\n // For other values, format them\n if (typeof value === \"string\") {\n // Multi-line strings get indented\n if (value.includes(\"\\n\")) {\n lines.push(value);\n } else {\n lines.push(value);\n }\n } else {\n lines.push(JSON.stringify(value));\n }\n }\n\n return lines.join(\"\\n\");\n}\n\n// User messages\nexport function logUserMessage(ctx: LogContext, text: string): void {\n console.log(chalk.green(`${timestamp()} ${formatContext(ctx)} ${text}`));\n}\n\n// Tool execution\nexport function logToolStart(\n ctx: LogContext,\n toolName: string,\n label: string,\n args: Record<string, unknown>,\n): void {\n const formattedArgs = formatToolArgs(args);\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ↳ ${toolName}: ${label}`));\n if (formattedArgs) {\n // Indent the args\n const indented = formattedArgs\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n }\n}\n\nexport function logToolSuccess(\n ctx: LogContext,\n toolName: string,\n durationMs: number,\n result: string,\n): void {\n const duration = (durationMs / 1000).toFixed(1);\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✓ ${toolName} (${duration}s)`));\n\n const truncated = truncate(result, LOG_PREVIEW_MAX);\n if (truncated) {\n const indented = truncated\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n }\n}\n\nexport function logToolError(\n ctx: LogContext,\n toolName: string,\n durationMs: number,\n error: string,\n): void {\n const duration = (durationMs / 1000).toFixed(1);\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} ✗ ${toolName} (${duration}s)`));\n\n const truncated = truncate(error, LOG_PREVIEW_MAX);\n const indented = truncated\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n}\n\n// Response streaming\nexport function logResponseStart(ctx: LogContext): void {\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} → Streaming response...`));\n}\n\nexport function logThinking(ctx: LogContext, thinking: string): void {\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💭 Thinking`));\n const truncated = truncate(thinking, LOG_PREVIEW_MAX);\n const indented = truncated\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n}\n\nexport function logResponse(ctx: LogContext, text: string): void {\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💬 Response`));\n const truncated = truncate(text, LOG_PREVIEW_MAX);\n const indented = truncated\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n}\n\n// System\nexport function logInfo(message: string): void {\n console.log(chalk.blue(`${timestamp()} [system] ${message}`));\n}\n\nexport function logWarning(message: string, details?: string): void {\n console.log(chalk.yellow(`${timestamp()} [system] ⚠ ${message}`));\n if (details) {\n const indented = details\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n }\n}\n\nexport function logAgentError(ctx: LogContext | \"system\", error: string): void {\n const context = ctx === \"system\" ? \"[system]\" : formatContext(ctx);\n console.log(chalk.yellow(`${timestamp()} ${context} ✗ Agent error`));\n const indented = error\n .split(\"\\n\")\n .map((line) => ` ${line}`)\n .join(\"\\n\");\n console.log(chalk.dim(indented));\n}\n\nfunction formatTokenCount(count: number): string {\n if (count < 1000) return count.toString();\n if (count < 10000) return `${(count / 1000).toFixed(1)}k`;\n if (count < 1000000) return `${Math.round(count / 1000)}k`;\n return `${(count / 1000000).toFixed(1)}M`;\n}\n\n// Usage summary\nexport function logUsageSummary(\n ctx: LogContext,\n usage: {\n input: number;\n output: number;\n cacheRead: number;\n cacheWrite: number;\n cost: { input: number; output: number; cacheRead: number; cacheWrite: number; total: number };\n },\n contextTokens?: number,\n contextWindow?: number,\n): string {\n const lines: string[] = [];\n lines.push(\"_Usage Summary_\");\n lines.push(`Tokens: ${usage.input.toLocaleString()} in, ${usage.output.toLocaleString()} out`);\n if (usage.cacheRead > 0 || usage.cacheWrite > 0) {\n lines.push(\n `Cache: ${usage.cacheRead.toLocaleString()} read, ${usage.cacheWrite.toLocaleString()} write`,\n );\n }\n if (contextTokens && contextWindow) {\n const contextPercent = ((contextTokens / contextWindow) * 100).toFixed(1);\n lines.push(\n `Context: ${formatTokenCount(contextTokens)} / ${formatTokenCount(contextWindow)} (${contextPercent}%)`,\n );\n }\n lines.push(\n `Cost: $${usage.cost.input.toFixed(4)} in, $${usage.cost.output.toFixed(4)} out` +\n (usage.cacheRead > 0 || usage.cacheWrite > 0\n ? `, $${usage.cost.cacheRead.toFixed(4)} cache read, $${usage.cost.cacheWrite.toFixed(4)} cache write`\n : \"\"),\n );\n lines.push(`*Total: $${usage.cost.total.toFixed(4)}*`);\n\n const summary = lines.join(\"\\n\");\n\n console.log(chalk.yellow(`${timestamp()} ${formatContext(ctx)} 💰 Usage`));\n console.log(\n chalk.dim(\n ` ${usage.input.toLocaleString()} in + ${usage.output.toLocaleString()} out` +\n (usage.cacheRead > 0 || usage.cacheWrite > 0\n ? ` (${usage.cacheRead.toLocaleString()} cache read, ${usage.cacheWrite.toLocaleString()} cache write)`\n : \"\") +\n ` = $${usage.cost.total.toFixed(4)}`,\n ),\n );\n\n return summary;\n}\n\n// Startup (no context needed)\nexport function logStartup(workingDir: string, sandbox: string): void {\n console.log(\"Starting mikan...\");\n console.log(` Working directory: ${workingDir}`);\n console.log(` Sandbox: ${sandbox}`);\n}\n\nexport function logConnected(platform: string): void {\n console.log(`⚡️ Mikan connected to ${platform} and listening!`);\n console.log(\"\");\n}\n\nexport function logDisconnected(): void {\n console.log(\"Mikan disconnected.\");\n}\n\n// Backfill\nexport function logBackfillStart(channelCount: number): void {\n console.log(chalk.blue(`${timestamp()} [system] Backfilling ${channelCount} channels...`));\n}\n\nexport function logBackfillChannel(channelName: string, messageCount: number): void {\n console.log(chalk.blue(`${timestamp()} [system] #${channelName}: ${messageCount} messages`));\n}\n\nexport function logBackfillComplete(totalMessages: number, durationMs: number): void {\n const duration = (durationMs / 1000).toFixed(1);\n console.log(\n chalk.blue(\n `${timestamp()} [system] Backfill complete: ${totalMessages} messages in ${duration}s`,\n ),\n );\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type LoginCredentialKind = "api_key" | "oauth";
|
|
2
|
+
export interface OAuthAuthorizedUserFileOutput {
|
|
3
|
+
type: "authorized_user";
|
|
4
|
+
relativePath: string;
|
|
5
|
+
targetPath?: string;
|
|
6
|
+
envKey?: string;
|
|
7
|
+
additionalEnvKeys?: string[];
|
|
8
|
+
}
|
|
9
|
+
export interface OAuthService {
|
|
10
|
+
id: string;
|
|
11
|
+
label: string;
|
|
12
|
+
aliases: string[];
|
|
13
|
+
authorizationUrl: string;
|
|
14
|
+
tokenUrl: string;
|
|
15
|
+
scopes: string[];
|
|
16
|
+
clientIdEnvKey: string;
|
|
17
|
+
clientSecretEnvKey: string;
|
|
18
|
+
accessTokenEnvKey?: string;
|
|
19
|
+
additionalAccessTokenEnvKeys?: string[];
|
|
20
|
+
refreshTokenEnvKey?: string;
|
|
21
|
+
authorizationParams?: Record<string, string>;
|
|
22
|
+
fileOutput?: OAuthAuthorizedUserFileOutput;
|
|
23
|
+
}
|
|
24
|
+
export type ParsedLoginCommand = {
|
|
25
|
+
command: "login" | "/login" | "/pi-login";
|
|
26
|
+
action: "setup";
|
|
27
|
+
} | {
|
|
28
|
+
command: "login" | "/login" | "/pi-login";
|
|
29
|
+
action: "shared_create" | "shared_update" | "shared_delete";
|
|
30
|
+
name: string;
|
|
31
|
+
} | {
|
|
32
|
+
command: "login" | "/login" | "/pi-login";
|
|
33
|
+
action: "shared_list";
|
|
34
|
+
} | {
|
|
35
|
+
command: "login" | "/login" | "/pi-login";
|
|
36
|
+
action: "copy_shared";
|
|
37
|
+
name: string;
|
|
38
|
+
};
|
|
39
|
+
export declare function getOAuthServices(): OAuthService[];
|
|
40
|
+
export declare function resolveOAuthService(input: string): OAuthService | undefined;
|
|
41
|
+
export declare function parseLoginCommand(text: string): ParsedLoginCommand | null;
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/login/index.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,OAAO,CAAC;AAEtD,MAAM,WAAW,6BAA6B;IAC5C,IAAI,EAAE,iBAAiB,CAAC;I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AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAM3E;AAID,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,GAAG,IAAI,CAgCzE","sourcesContent":["import { matchCommand } from \"../commands/parse.js\";\nimport { readEnv } from \"../env.js\";\nimport { isRecord, parseJsonValue } from \"../file-guards.js\";\nimport * as log from \"../log.js\";\n\nexport type LoginCredentialKind = \"api_key\" | \"oauth\";\n\nexport interface OAuthAuthorizedUserFileOutput {\n type: \"authorized_user\";\n relativePath: string;\n targetPath?: string;\n envKey?: string;\n additionalEnvKeys?: string[];\n}\n\nexport interface OAuthService {\n id: string;\n label: string;\n aliases: string[];\n authorizationUrl: string;\n tokenUrl: string;\n scopes: string[];\n clientIdEnvKey: string;\n clientSecretEnvKey: string;\n accessTokenEnvKey?: string;\n additionalAccessTokenEnvKeys?: string[];\n refreshTokenEnvKey?: string;\n authorizationParams?: Record<string, string>;\n fileOutput?: OAuthAuthorizedUserFileOutput;\n}\n\nexport type ParsedLoginCommand =\n | { command: \"login\" | \"/login\" | \"/pi-login\"; action: \"setup\" }\n | {\n command: \"login\" | \"/login\" | \"/pi-login\";\n action: \"shared_create\" | \"shared_update\" | \"shared_delete\";\n name: string;\n }\n | { command: \"login\" | \"/login\" | \"/pi-login\"; action: \"shared_list\" }\n | { command: \"login\" | \"/login\" | \"/pi-login\"; action: \"copy_shared\"; name: string };\n\nconst DEFAULT_GOOGLE_WORKSPACE_CLI_SCOPES = [\n \"https://www.googleapis.com/auth/drive\",\n \"https://mail.google.com/\",\n \"https://www.googleapis.com/auth/calendar\",\n \"https://www.googleapis.com/auth/spreadsheets\",\n \"https://www.googleapis.com/auth/documents\",\n \"https://www.googleapis.com/auth/chat.messages.create\",\n];\n\nconst DEFAULT_GOOGLE_CLOUD_SDK_SCOPES = [\n \"openid\",\n \"https://www.googleapis.com/auth/userinfo.email\",\n \"https://www.googleapis.com/auth/cloud-platform\",\n];\n\n// Conservative default: enough for `gh` CLI repo/user/org operations, but\n// without `workflow` (can dispatch CI), `write:packages` (can publish\n// packages), or `project`. Operators who need those can opt in via\n// GITHUB_OAUTH_SCOPES (or MIKAN_GITHUB_OAUTH_SCOPES) to keep the blast radius\n// of a compromised agent host explicit and configurable.\nconst DEFAULT_GITHUB_OAUTH_SCOPES = [\"repo\", \"read:user\", \"user:email\", \"read:org\", \"gist\"];\n\nfunction resolveScopesFromEnv(envKey: string, fallback: string[]): string[] {\n const raw = readEnv(envKey);\n if (!raw) return fallback;\n\n const scopes = raw\n .split(/[\\s,]+/)\n .map((scope) => scope.trim())\n .filter(Boolean);\n\n return scopes.length > 0 ? scopes : fallback;\n}\n\nfunction resolveGoogleWorkspaceCliScopes(): string[] {\n return resolveScopesFromEnv(\n \"GOOGLE_WORKSPACE_CLI_OAUTH_SCOPES\",\n DEFAULT_GOOGLE_WORKSPACE_CLI_SCOPES,\n );\n}\n\nfunction resolveGoogleCloudSdkScopes(): string[] {\n return resolveScopesFromEnv(\"GOOGLE_CLOUD_SDK_OAUTH_SCOPES\", DEFAULT_GOOGLE_CLOUD_SDK_SCOPES);\n}\n\nfunction resolveGitHubOAuthScopes(): string[] {\n return resolveScopesFromEnv(\"GITHUB_OAUTH_SCOPES\", DEFAULT_GITHUB_OAUTH_SCOPES);\n}\n\nfunction getBuiltinOAuthServices(): OAuthService[] {\n return [\n {\n id: \"github\",\n label: \"GitHub\",\n aliases: [\"github\", \"github_oauth\", \"gh_oauth\"],\n authorizationUrl: \"https://github.com/login/oauth/authorize\",\n tokenUrl: \"https://github.com/login/oauth/access_token\",\n scopes: resolveGitHubOAuthScopes(),\n clientIdEnvKey: \"GITHUB_OAUTH_CLIENT_ID\",\n clientSecretEnvKey: \"GITHUB_OAUTH_CLIENT_SECRET\",\n accessTokenEnvKey: \"GITHUB_OAUTH_ACCESS_TOKEN\",\n additionalAccessTokenEnvKeys: [\"GH_TOKEN\"],\n refreshTokenEnvKey: \"GITHUB_OAUTH_REFRESH_TOKEN\",\n },\n {\n id: \"google_workspace_cli\",\n label: \"Google Workspace CLI\",\n aliases: [\"google_workspace_cli\", \"gws\", \"googleworkspace\", \"google-workspace-cli\"],\n authorizationUrl: \"https://accounts.google.com/o/oauth2/v2/auth\",\n tokenUrl: \"https://oauth2.googleapis.com/token\",\n scopes: resolveGoogleWorkspaceCliScopes(),\n clientIdEnvKey: \"GOOGLE_WORKSPACE_CLI_CLIENT_ID\",\n clientSecretEnvKey: \"GOOGLE_WORKSPACE_CLI_CLIENT_SECRET\",\n authorizationParams: {\n access_type: \"offline\",\n include_granted_scopes: \"true\",\n prompt: \"consent\",\n },\n fileOutput: {\n type: \"authorized_user\",\n relativePath: \"gws.json\",\n targetPath: \"/root/.config/gws/credentials.json\",\n },\n },\n {\n id: \"google_cloud_sdk\",\n label: \"Google Cloud SDK (gcloud)\",\n aliases: [\"google_cloud_sdk\", \"gcloud\", \"google-cloud-sdk\", \"google_cloud\", \"gcp\"],\n authorizationUrl: \"https://accounts.google.com/o/oauth2/v2/auth\",\n tokenUrl: \"https://oauth2.googleapis.com/token\",\n scopes: resolveGoogleCloudSdkScopes(),\n clientIdEnvKey: \"GOOGLE_CLOUD_SDK_CLIENT_ID\",\n clientSecretEnvKey: \"GOOGLE_CLOUD_SDK_CLIENT_SECRET\",\n authorizationParams: {\n access_type: \"offline\",\n include_granted_scopes: \"true\",\n prompt: \"consent\",\n },\n fileOutput: {\n type: \"authorized_user\",\n relativePath: \"gcloud-adc.json\",\n targetPath: \"/root/.config/gcloud/application_default_credentials.json\",\n envKey: \"GOOGLE_APPLICATION_CREDENTIALS\",\n additionalEnvKeys: [\"CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE\"],\n },\n },\n ];\n}\n\nexport function getOAuthServices(): OAuthService[] {\n const raw = readEnv(\"OAUTH_SERVICES_JSON\");\n const builtins = getBuiltinOAuthServices();\n if (!raw) return builtins;\n\n let parsed: unknown[];\n try {\n parsed = parseJsonValue(raw, Array.isArray, (detail) =>\n detail === \"unexpected JSON shape\"\n ? \"expected a JSON array of OAuth service definitions\"\n : detail,\n );\n } catch (err) {\n const detail = err instanceof Error ? err.message : String(err);\n log.logWarning(\n detail === \"expected a JSON array of OAuth service definitions\"\n ? \"Ignoring OAUTH_SERVICES_JSON: expected a JSON array of OAuth service definitions\"\n : \"Ignoring OAUTH_SERVICES_JSON: invalid JSON\",\n detail,\n );\n return builtins;\n }\n try {\n const custom = parsed\n .map((serviceValue): OAuthService | null => {\n if (!isRecord(serviceValue)) return null;\n const obj = serviceValue;\n const id = typeof obj.id === \"string\" ? obj.id.trim() : \"\";\n const label = typeof obj.label === \"string\" ? obj.label.trim() : \"\";\n const authorizationUrl =\n typeof obj.authorizationUrl === \"string\" ? obj.authorizationUrl.trim() : \"\";\n const tokenUrl = typeof obj.tokenUrl === \"string\" ? obj.tokenUrl.trim() : \"\";\n const clientIdEnvKey =\n typeof obj.clientIdEnvKey === \"string\" ? obj.clientIdEnvKey.trim() : \"\";\n const clientSecretEnvKey =\n typeof obj.clientSecretEnvKey === \"string\" ? obj.clientSecretEnvKey.trim() : \"\";\n const accessTokenEnvKey =\n typeof obj.accessTokenEnvKey === \"string\" ? obj.accessTokenEnvKey.trim() : undefined;\n if (\n !id ||\n !label ||\n !authorizationUrl ||\n !tokenUrl ||\n !clientIdEnvKey ||\n !clientSecretEnvKey\n ) {\n return null;\n }\n\n let fileOutput: OAuthService[\"fileOutput\"];\n if (isRecord(obj.fileOutput)) {\n const fileOutputObj = obj.fileOutput;\n const type = typeof fileOutputObj.type === \"string\" ? fileOutputObj.type.trim() : \"\";\n const relativePath =\n typeof fileOutputObj.relativePath === \"string\" ? fileOutputObj.relativePath.trim() : \"\";\n const targetPath =\n typeof fileOutputObj.targetPath === \"string\"\n ? fileOutputObj.targetPath.trim()\n : undefined;\n const envKey =\n typeof fileOutputObj.envKey === \"string\" ? fileOutputObj.envKey.trim() : undefined;\n const additionalEnvKeys = Array.isArray(fileOutputObj.additionalEnvKeys)\n ? fileOutputObj.additionalEnvKeys.filter((v): v is string => typeof v === \"string\")\n : undefined;\n if (type === \"authorized_user\" && relativePath) {\n fileOutput = {\n type: \"authorized_user\",\n relativePath,\n targetPath,\n envKey,\n additionalEnvKeys,\n };\n }\n }\n\n return {\n id: id.toLowerCase(),\n label,\n aliases: Array.isArray(obj.aliases)\n ? obj.aliases\n .filter((v): v is string => typeof v === \"string\")\n .map((v) => v.toLowerCase())\n : [id.toLowerCase()],\n authorizationUrl,\n tokenUrl,\n scopes: Array.isArray(obj.scopes)\n ? obj.scopes.filter((v): v is string => typeof v === \"string\")\n : [],\n clientIdEnvKey,\n clientSecretEnvKey,\n accessTokenEnvKey,\n additionalAccessTokenEnvKeys: Array.isArray(obj.additionalAccessTokenEnvKeys)\n ? obj.additionalAccessTokenEnvKeys.filter((v): v is string => typeof v === \"string\")\n : undefined,\n refreshTokenEnvKey:\n typeof obj.refreshTokenEnvKey === \"string\" ? obj.refreshTokenEnvKey.trim() : undefined,\n authorizationParams: isRecord(obj.authorizationParams)\n ? Object.fromEntries(\n Object.entries(obj.authorizationParams).filter(\n (authorizationEntry): authorizationEntry is [string, string] =>\n typeof authorizationEntry[1] === \"string\",\n ),\n )\n : undefined,\n fileOutput,\n };\n })\n .filter((service): service is OAuthService => service !== null);\n\n const byId = new Map<string, OAuthService>();\n for (const service of builtins) byId.set(service.id, service);\n for (const service of custom) byId.set(service.id, service);\n return [...byId.values()];\n } catch (err) {\n log.logWarning(\n \"Failed to apply OAUTH_SERVICES_JSON overrides; using builtin OAuth services\",\n err instanceof Error ? err.message : String(err),\n );\n return builtins;\n }\n}\n\nexport function resolveOAuthService(input: string): OAuthService | undefined {\n const normalized = input.trim().toLowerCase();\n if (!normalized) return undefined;\n return getOAuthServices().find(\n (service) => service.id === normalized || service.aliases.includes(normalized),\n );\n}\n\nconst LOGIN_COMMANDS = [\"login\", \"/login\", \"/pi-login\"] as const;\n\nexport function parseLoginCommand(text: string): ParsedLoginCommand | null {\n const matched = matchCommand(text, LOGIN_COMMANDS);\n if (!matched) return null;\n\n const [subcommand, operation, name, ...extra] = matched.args;\n\n if (!subcommand) return { command: matched.command, action: \"setup\" };\n\n if (subcommand.toLowerCase() === \"shared\") {\n const op = operation?.toLowerCase();\n if (op === \"list\" && !name && extra.length === 0) {\n return { command: matched.command, action: \"shared_list\" };\n }\n if ((op === \"create\" || op === \"update\" || op === \"delete\") && !!name && extra.length === 0) {\n return {\n command: matched.command,\n action: `shared_${op}` as \"shared_create\" | \"shared_update\" | \"shared_delete\",\n name,\n };\n }\n return null;\n }\n\n if (subcommand.toLowerCase() === \"copy\" && operation && !name && extra.length === 0) {\n return { command: matched.command, action: \"copy_shared\", name: operation };\n }\n\n // Backward-compatible: older `/pi-login gh` / `/pi-login gws` forms opened the\n // generic login page and let the portal handle provider choice.\n if (!operation && extra.length === 0) return { command: matched.command, action: \"setup\" };\n\n return null;\n}\n"]}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { matchCommand } from "../commands/parse.js";
|
|
2
|
+
import { readEnv } from "../env.js";
|
|
3
|
+
import { isRecord, parseJsonValue } from "../file-guards.js";
|
|
4
|
+
import * as log from "../log.js";
|
|
5
|
+
const DEFAULT_GOOGLE_WORKSPACE_CLI_SCOPES = [
|
|
6
|
+
"https://www.googleapis.com/auth/drive",
|
|
7
|
+
"https://mail.google.com/",
|
|
8
|
+
"https://www.googleapis.com/auth/calendar",
|
|
9
|
+
"https://www.googleapis.com/auth/spreadsheets",
|
|
10
|
+
"https://www.googleapis.com/auth/documents",
|
|
11
|
+
"https://www.googleapis.com/auth/chat.messages.create",
|
|
12
|
+
];
|
|
13
|
+
const DEFAULT_GOOGLE_CLOUD_SDK_SCOPES = [
|
|
14
|
+
"openid",
|
|
15
|
+
"https://www.googleapis.com/auth/userinfo.email",
|
|
16
|
+
"https://www.googleapis.com/auth/cloud-platform",
|
|
17
|
+
];
|
|
18
|
+
// Conservative default: enough for `gh` CLI repo/user/org operations, but
|
|
19
|
+
// without `workflow` (can dispatch CI), `write:packages` (can publish
|
|
20
|
+
// packages), or `project`. Operators who need those can opt in via
|
|
21
|
+
// GITHUB_OAUTH_SCOPES (or MIKAN_GITHUB_OAUTH_SCOPES) to keep the blast radius
|
|
22
|
+
// of a compromised agent host explicit and configurable.
|
|
23
|
+
const DEFAULT_GITHUB_OAUTH_SCOPES = ["repo", "read:user", "user:email", "read:org", "gist"];
|
|
24
|
+
function resolveScopesFromEnv(envKey, fallback) {
|
|
25
|
+
const raw = readEnv(envKey);
|
|
26
|
+
if (!raw)
|
|
27
|
+
return fallback;
|
|
28
|
+
const scopes = raw
|
|
29
|
+
.split(/[\s,]+/)
|
|
30
|
+
.map((scope) => scope.trim())
|
|
31
|
+
.filter(Boolean);
|
|
32
|
+
return scopes.length > 0 ? scopes : fallback;
|
|
33
|
+
}
|
|
34
|
+
function resolveGoogleWorkspaceCliScopes() {
|
|
35
|
+
return resolveScopesFromEnv("GOOGLE_WORKSPACE_CLI_OAUTH_SCOPES", DEFAULT_GOOGLE_WORKSPACE_CLI_SCOPES);
|
|
36
|
+
}
|
|
37
|
+
function resolveGoogleCloudSdkScopes() {
|
|
38
|
+
return resolveScopesFromEnv("GOOGLE_CLOUD_SDK_OAUTH_SCOPES", DEFAULT_GOOGLE_CLOUD_SDK_SCOPES);
|
|
39
|
+
}
|
|
40
|
+
function resolveGitHubOAuthScopes() {
|
|
41
|
+
return resolveScopesFromEnv("GITHUB_OAUTH_SCOPES", DEFAULT_GITHUB_OAUTH_SCOPES);
|
|
42
|
+
}
|
|
43
|
+
function getBuiltinOAuthServices() {
|
|
44
|
+
return [
|
|
45
|
+
{
|
|
46
|
+
id: "github",
|
|
47
|
+
label: "GitHub",
|
|
48
|
+
aliases: ["github", "github_oauth", "gh_oauth"],
|
|
49
|
+
authorizationUrl: "https://github.com/login/oauth/authorize",
|
|
50
|
+
tokenUrl: "https://github.com/login/oauth/access_token",
|
|
51
|
+
scopes: resolveGitHubOAuthScopes(),
|
|
52
|
+
clientIdEnvKey: "GITHUB_OAUTH_CLIENT_ID",
|
|
53
|
+
clientSecretEnvKey: "GITHUB_OAUTH_CLIENT_SECRET",
|
|
54
|
+
accessTokenEnvKey: "GITHUB_OAUTH_ACCESS_TOKEN",
|
|
55
|
+
additionalAccessTokenEnvKeys: ["GH_TOKEN"],
|
|
56
|
+
refreshTokenEnvKey: "GITHUB_OAUTH_REFRESH_TOKEN",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: "google_workspace_cli",
|
|
60
|
+
label: "Google Workspace CLI",
|
|
61
|
+
aliases: ["google_workspace_cli", "gws", "googleworkspace", "google-workspace-cli"],
|
|
62
|
+
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
|
|
63
|
+
tokenUrl: "https://oauth2.googleapis.com/token",
|
|
64
|
+
scopes: resolveGoogleWorkspaceCliScopes(),
|
|
65
|
+
clientIdEnvKey: "GOOGLE_WORKSPACE_CLI_CLIENT_ID",
|
|
66
|
+
clientSecretEnvKey: "GOOGLE_WORKSPACE_CLI_CLIENT_SECRET",
|
|
67
|
+
authorizationParams: {
|
|
68
|
+
access_type: "offline",
|
|
69
|
+
include_granted_scopes: "true",
|
|
70
|
+
prompt: "consent",
|
|
71
|
+
},
|
|
72
|
+
fileOutput: {
|
|
73
|
+
type: "authorized_user",
|
|
74
|
+
relativePath: "gws.json",
|
|
75
|
+
targetPath: "/root/.config/gws/credentials.json",
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "google_cloud_sdk",
|
|
80
|
+
label: "Google Cloud SDK (gcloud)",
|
|
81
|
+
aliases: ["google_cloud_sdk", "gcloud", "google-cloud-sdk", "google_cloud", "gcp"],
|
|
82
|
+
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
|
|
83
|
+
tokenUrl: "https://oauth2.googleapis.com/token",
|
|
84
|
+
scopes: resolveGoogleCloudSdkScopes(),
|
|
85
|
+
clientIdEnvKey: "GOOGLE_CLOUD_SDK_CLIENT_ID",
|
|
86
|
+
clientSecretEnvKey: "GOOGLE_CLOUD_SDK_CLIENT_SECRET",
|
|
87
|
+
authorizationParams: {
|
|
88
|
+
access_type: "offline",
|
|
89
|
+
include_granted_scopes: "true",
|
|
90
|
+
prompt: "consent",
|
|
91
|
+
},
|
|
92
|
+
fileOutput: {
|
|
93
|
+
type: "authorized_user",
|
|
94
|
+
relativePath: "gcloud-adc.json",
|
|
95
|
+
targetPath: "/root/.config/gcloud/application_default_credentials.json",
|
|
96
|
+
envKey: "GOOGLE_APPLICATION_CREDENTIALS",
|
|
97
|
+
additionalEnvKeys: ["CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE"],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
export function getOAuthServices() {
|
|
103
|
+
const raw = readEnv("OAUTH_SERVICES_JSON");
|
|
104
|
+
const builtins = getBuiltinOAuthServices();
|
|
105
|
+
if (!raw)
|
|
106
|
+
return builtins;
|
|
107
|
+
let parsed;
|
|
108
|
+
try {
|
|
109
|
+
parsed = parseJsonValue(raw, Array.isArray, (detail) => detail === "unexpected JSON shape"
|
|
110
|
+
? "expected a JSON array of OAuth service definitions"
|
|
111
|
+
: detail);
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
115
|
+
log.logWarning(detail === "expected a JSON array of OAuth service definitions"
|
|
116
|
+
? "Ignoring OAUTH_SERVICES_JSON: expected a JSON array of OAuth service definitions"
|
|
117
|
+
: "Ignoring OAUTH_SERVICES_JSON: invalid JSON", detail);
|
|
118
|
+
return builtins;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const custom = parsed
|
|
122
|
+
.map((serviceValue) => {
|
|
123
|
+
if (!isRecord(serviceValue))
|
|
124
|
+
return null;
|
|
125
|
+
const obj = serviceValue;
|
|
126
|
+
const id = typeof obj.id === "string" ? obj.id.trim() : "";
|
|
127
|
+
const label = typeof obj.label === "string" ? obj.label.trim() : "";
|
|
128
|
+
const authorizationUrl = typeof obj.authorizationUrl === "string" ? obj.authorizationUrl.trim() : "";
|
|
129
|
+
const tokenUrl = typeof obj.tokenUrl === "string" ? obj.tokenUrl.trim() : "";
|
|
130
|
+
const clientIdEnvKey = typeof obj.clientIdEnvKey === "string" ? obj.clientIdEnvKey.trim() : "";
|
|
131
|
+
const clientSecretEnvKey = typeof obj.clientSecretEnvKey === "string" ? obj.clientSecretEnvKey.trim() : "";
|
|
132
|
+
const accessTokenEnvKey = typeof obj.accessTokenEnvKey === "string" ? obj.accessTokenEnvKey.trim() : undefined;
|
|
133
|
+
if (!id ||
|
|
134
|
+
!label ||
|
|
135
|
+
!authorizationUrl ||
|
|
136
|
+
!tokenUrl ||
|
|
137
|
+
!clientIdEnvKey ||
|
|
138
|
+
!clientSecretEnvKey) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
let fileOutput;
|
|
142
|
+
if (isRecord(obj.fileOutput)) {
|
|
143
|
+
const fileOutputObj = obj.fileOutput;
|
|
144
|
+
const type = typeof fileOutputObj.type === "string" ? fileOutputObj.type.trim() : "";
|
|
145
|
+
const relativePath = typeof fileOutputObj.relativePath === "string" ? fileOutputObj.relativePath.trim() : "";
|
|
146
|
+
const targetPath = typeof fileOutputObj.targetPath === "string"
|
|
147
|
+
? fileOutputObj.targetPath.trim()
|
|
148
|
+
: undefined;
|
|
149
|
+
const envKey = typeof fileOutputObj.envKey === "string" ? fileOutputObj.envKey.trim() : undefined;
|
|
150
|
+
const additionalEnvKeys = Array.isArray(fileOutputObj.additionalEnvKeys)
|
|
151
|
+
? fileOutputObj.additionalEnvKeys.filter((v) => typeof v === "string")
|
|
152
|
+
: undefined;
|
|
153
|
+
if (type === "authorized_user" && relativePath) {
|
|
154
|
+
fileOutput = {
|
|
155
|
+
type: "authorized_user",
|
|
156
|
+
relativePath,
|
|
157
|
+
targetPath,
|
|
158
|
+
envKey,
|
|
159
|
+
additionalEnvKeys,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
id: id.toLowerCase(),
|
|
165
|
+
label,
|
|
166
|
+
aliases: Array.isArray(obj.aliases)
|
|
167
|
+
? obj.aliases
|
|
168
|
+
.filter((v) => typeof v === "string")
|
|
169
|
+
.map((v) => v.toLowerCase())
|
|
170
|
+
: [id.toLowerCase()],
|
|
171
|
+
authorizationUrl,
|
|
172
|
+
tokenUrl,
|
|
173
|
+
scopes: Array.isArray(obj.scopes)
|
|
174
|
+
? obj.scopes.filter((v) => typeof v === "string")
|
|
175
|
+
: [],
|
|
176
|
+
clientIdEnvKey,
|
|
177
|
+
clientSecretEnvKey,
|
|
178
|
+
accessTokenEnvKey,
|
|
179
|
+
additionalAccessTokenEnvKeys: Array.isArray(obj.additionalAccessTokenEnvKeys)
|
|
180
|
+
? obj.additionalAccessTokenEnvKeys.filter((v) => typeof v === "string")
|
|
181
|
+
: undefined,
|
|
182
|
+
refreshTokenEnvKey: typeof obj.refreshTokenEnvKey === "string" ? obj.refreshTokenEnvKey.trim() : undefined,
|
|
183
|
+
authorizationParams: isRecord(obj.authorizationParams)
|
|
184
|
+
? Object.fromEntries(Object.entries(obj.authorizationParams).filter((authorizationEntry) => typeof authorizationEntry[1] === "string"))
|
|
185
|
+
: undefined,
|
|
186
|
+
fileOutput,
|
|
187
|
+
};
|
|
188
|
+
})
|
|
189
|
+
.filter((service) => service !== null);
|
|
190
|
+
const byId = new Map();
|
|
191
|
+
for (const service of builtins)
|
|
192
|
+
byId.set(service.id, service);
|
|
193
|
+
for (const service of custom)
|
|
194
|
+
byId.set(service.id, service);
|
|
195
|
+
return [...byId.values()];
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
log.logWarning("Failed to apply OAUTH_SERVICES_JSON overrides; using builtin OAuth services", err instanceof Error ? err.message : String(err));
|
|
199
|
+
return builtins;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export function resolveOAuthService(input) {
|
|
203
|
+
const normalized = input.trim().toLowerCase();
|
|
204
|
+
if (!normalized)
|
|
205
|
+
return undefined;
|
|
206
|
+
return getOAuthServices().find((service) => service.id === normalized || service.aliases.includes(normalized));
|
|
207
|
+
}
|
|
208
|
+
const LOGIN_COMMANDS = ["login", "/login", "/pi-login"];
|
|
209
|
+
export function parseLoginCommand(text) {
|
|
210
|
+
const matched = matchCommand(text, LOGIN_COMMANDS);
|
|
211
|
+
if (!matched)
|
|
212
|
+
return null;
|
|
213
|
+
const [subcommand, operation, name, ...extra] = matched.args;
|
|
214
|
+
if (!subcommand)
|
|
215
|
+
return { command: matched.command, action: "setup" };
|
|
216
|
+
if (subcommand.toLowerCase() === "shared") {
|
|
217
|
+
const op = operation?.toLowerCase();
|
|
218
|
+
if (op === "list" && !name && extra.length === 0) {
|
|
219
|
+
return { command: matched.command, action: "shared_list" };
|
|
220
|
+
}
|
|
221
|
+
if ((op === "create" || op === "update" || op === "delete") && !!name && extra.length === 0) {
|
|
222
|
+
return {
|
|
223
|
+
command: matched.command,
|
|
224
|
+
action: `shared_${op}`,
|
|
225
|
+
name,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
if (subcommand.toLowerCase() === "copy" && operation && !name && extra.length === 0) {
|
|
231
|
+
return { command: matched.command, action: "copy_shared", name: operation };
|
|
232
|
+
}
|
|
233
|
+
// Backward-compatible: older `/pi-login gh` / `/pi-login gws` forms opened the
|
|
234
|
+
// generic login page and let the portal handle provider choice.
|
|
235
|
+
if (!operation && extra.length === 0)
|
|
236
|
+
return { command: matched.command, action: "setup" };
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/login/index.ts"],"names":[],"mappings":"AAA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AK,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAE3F,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { matchCommand } from \"../commands/parse.js\";\nimport { readEnv } from \"../env.js\";\nimport { isRecord, parseJsonValue } from \"../file-guards.js\";\nimport * as log from \"../log.js\";\n\nexport type LoginCredentialKind = \"api_key\" | \"oauth\";\n\nexport interface OAuthAuthorizedUserFileOutput {\n type: \"authorized_user\";\n relativePath: string;\n targetPath?: string;\n envKey?: string;\n additionalEnvKeys?: string[];\n}\n\nexport interface OAuthService {\n id: string;\n label: string;\n aliases: string[];\n authorizationUrl: string;\n tokenUrl: string;\n scopes: string[];\n clientIdEnvKey: string;\n clientSecretEnvKey: string;\n accessTokenEnvKey?: string;\n additionalAccessTokenEnvKeys?: string[];\n refreshTokenEnvKey?: string;\n authorizationParams?: Record<string, string>;\n fileOutput?: OAuthAuthorizedUserFileOutput;\n}\n\nexport type ParsedLoginCommand =\n | { command: \"login\" | \"/login\" | \"/pi-login\"; action: \"setup\" }\n | {\n command: \"login\" | \"/login\" | \"/pi-login\";\n action: \"shared_create\" | \"shared_update\" | \"shared_delete\";\n name: string;\n }\n | { command: \"login\" | \"/login\" | \"/pi-login\"; action: \"shared_list\" }\n | { command: \"login\" | \"/login\" | \"/pi-login\"; action: \"copy_shared\"; name: string };\n\nconst DEFAULT_GOOGLE_WORKSPACE_CLI_SCOPES = [\n \"https://www.googleapis.com/auth/drive\",\n \"https://mail.google.com/\",\n \"https://www.googleapis.com/auth/calendar\",\n \"https://www.googleapis.com/auth/spreadsheets\",\n \"https://www.googleapis.com/auth/documents\",\n \"https://www.googleapis.com/auth/chat.messages.create\",\n];\n\nconst DEFAULT_GOOGLE_CLOUD_SDK_SCOPES = [\n \"openid\",\n \"https://www.googleapis.com/auth/userinfo.email\",\n \"https://www.googleapis.com/auth/cloud-platform\",\n];\n\n// Conservative default: enough for `gh` CLI repo/user/org operations, but\n// without `workflow` (can dispatch CI), `write:packages` (can publish\n// packages), or `project`. Operators who need those can opt in via\n// GITHUB_OAUTH_SCOPES (or MIKAN_GITHUB_OAUTH_SCOPES) to keep the blast radius\n// of a compromised agent host explicit and configurable.\nconst DEFAULT_GITHUB_OAUTH_SCOPES = [\"repo\", \"read:user\", \"user:email\", \"read:org\", \"gist\"];\n\nfunction resolveScopesFromEnv(envKey: string, fallback: string[]): string[] {\n const raw = readEnv(envKey);\n if (!raw) return fallback;\n\n const scopes = raw\n .split(/[\\s,]+/)\n .map((scope) => scope.trim())\n .filter(Boolean);\n\n return scopes.length > 0 ? scopes : fallback;\n}\n\nfunction resolveGoogleWorkspaceCliScopes(): string[] {\n return resolveScopesFromEnv(\n \"GOOGLE_WORKSPACE_CLI_OAUTH_SCOPES\",\n DEFAULT_GOOGLE_WORKSPACE_CLI_SCOPES,\n );\n}\n\nfunction resolveGoogleCloudSdkScopes(): string[] {\n return resolveScopesFromEnv(\"GOOGLE_CLOUD_SDK_OAUTH_SCOPES\", DEFAULT_GOOGLE_CLOUD_SDK_SCOPES);\n}\n\nfunction resolveGitHubOAuthScopes(): string[] {\n return resolveScopesFromEnv(\"GITHUB_OAUTH_SCOPES\", DEFAULT_GITHUB_OAUTH_SCOPES);\n}\n\nfunction getBuiltinOAuthServices(): OAuthService[] {\n return [\n {\n id: \"github\",\n label: \"GitHub\",\n aliases: [\"github\", \"github_oauth\", \"gh_oauth\"],\n authorizationUrl: \"https://github.com/login/oauth/authorize\",\n tokenUrl: \"https://github.com/login/oauth/access_token\",\n scopes: resolveGitHubOAuthScopes(),\n clientIdEnvKey: \"GITHUB_OAUTH_CLIENT_ID\",\n clientSecretEnvKey: \"GITHUB_OAUTH_CLIENT_SECRET\",\n accessTokenEnvKey: \"GITHUB_OAUTH_ACCESS_TOKEN\",\n additionalAccessTokenEnvKeys: [\"GH_TOKEN\"],\n refreshTokenEnvKey: \"GITHUB_OAUTH_REFRESH_TOKEN\",\n },\n {\n id: \"google_workspace_cli\",\n label: \"Google Workspace CLI\",\n aliases: [\"google_workspace_cli\", \"gws\", \"googleworkspace\", \"google-workspace-cli\"],\n authorizationUrl: \"https://accounts.google.com/o/oauth2/v2/auth\",\n tokenUrl: \"https://oauth2.googleapis.com/token\",\n scopes: resolveGoogleWorkspaceCliScopes(),\n clientIdEnvKey: \"GOOGLE_WORKSPACE_CLI_CLIENT_ID\",\n clientSecretEnvKey: \"GOOGLE_WORKSPACE_CLI_CLIENT_SECRET\",\n authorizationParams: {\n access_type: \"offline\",\n include_granted_scopes: \"true\",\n prompt: \"consent\",\n },\n fileOutput: {\n type: \"authorized_user\",\n relativePath: \"gws.json\",\n targetPath: \"/root/.config/gws/credentials.json\",\n },\n },\n {\n id: \"google_cloud_sdk\",\n label: \"Google Cloud SDK (gcloud)\",\n aliases: [\"google_cloud_sdk\", \"gcloud\", \"google-cloud-sdk\", \"google_cloud\", \"gcp\"],\n authorizationUrl: \"https://accounts.google.com/o/oauth2/v2/auth\",\n tokenUrl: \"https://oauth2.googleapis.com/token\",\n scopes: resolveGoogleCloudSdkScopes(),\n clientIdEnvKey: \"GOOGLE_CLOUD_SDK_CLIENT_ID\",\n clientSecretEnvKey: \"GOOGLE_CLOUD_SDK_CLIENT_SECRET\",\n authorizationParams: {\n access_type: \"offline\",\n include_granted_scopes: \"true\",\n prompt: \"consent\",\n },\n fileOutput: {\n type: \"authorized_user\",\n relativePath: \"gcloud-adc.json\",\n targetPath: \"/root/.config/gcloud/application_default_credentials.json\",\n envKey: \"GOOGLE_APPLICATION_CREDENTIALS\",\n additionalEnvKeys: [\"CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE\"],\n },\n },\n ];\n}\n\nexport function getOAuthServices(): OAuthService[] {\n const raw = readEnv(\"OAUTH_SERVICES_JSON\");\n const builtins = getBuiltinOAuthServices();\n if (!raw) return builtins;\n\n let parsed: unknown[];\n try {\n parsed = parseJsonValue(raw, Array.isArray, (detail) =>\n detail === \"unexpected JSON shape\"\n ? \"expected a JSON array of OAuth service definitions\"\n : detail,\n );\n } catch (err) {\n const detail = err instanceof Error ? err.message : String(err);\n log.logWarning(\n detail === \"expected a JSON array of OAuth service definitions\"\n ? \"Ignoring OAUTH_SERVICES_JSON: expected a JSON array of OAuth service definitions\"\n : \"Ignoring OAUTH_SERVICES_JSON: invalid JSON\",\n detail,\n );\n return builtins;\n }\n try {\n const custom = parsed\n .map((serviceValue): OAuthService | null => {\n if (!isRecord(serviceValue)) return null;\n const obj = serviceValue;\n const id = typeof obj.id === \"string\" ? obj.id.trim() : \"\";\n const label = typeof obj.label === \"string\" ? obj.label.trim() : \"\";\n const authorizationUrl =\n typeof obj.authorizationUrl === \"string\" ? obj.authorizationUrl.trim() : \"\";\n const tokenUrl = typeof obj.tokenUrl === \"string\" ? obj.tokenUrl.trim() : \"\";\n const clientIdEnvKey =\n typeof obj.clientIdEnvKey === \"string\" ? obj.clientIdEnvKey.trim() : \"\";\n const clientSecretEnvKey =\n typeof obj.clientSecretEnvKey === \"string\" ? obj.clientSecretEnvKey.trim() : \"\";\n const accessTokenEnvKey =\n typeof obj.accessTokenEnvKey === \"string\" ? obj.accessTokenEnvKey.trim() : undefined;\n if (\n !id ||\n !label ||\n !authorizationUrl ||\n !tokenUrl ||\n !clientIdEnvKey ||\n !clientSecretEnvKey\n ) {\n return null;\n }\n\n let fileOutput: OAuthService[\"fileOutput\"];\n if (isRecord(obj.fileOutput)) {\n const fileOutputObj = obj.fileOutput;\n const type = typeof fileOutputObj.type === \"string\" ? fileOutputObj.type.trim() : \"\";\n const relativePath =\n typeof fileOutputObj.relativePath === \"string\" ? fileOutputObj.relativePath.trim() : \"\";\n const targetPath =\n typeof fileOutputObj.targetPath === \"string\"\n ? fileOutputObj.targetPath.trim()\n : undefined;\n const envKey =\n typeof fileOutputObj.envKey === \"string\" ? fileOutputObj.envKey.trim() : undefined;\n const additionalEnvKeys = Array.isArray(fileOutputObj.additionalEnvKeys)\n ? fileOutputObj.additionalEnvKeys.filter((v): v is string => typeof v === \"string\")\n : undefined;\n if (type === \"authorized_user\" && relativePath) {\n fileOutput = {\n type: \"authorized_user\",\n relativePath,\n targetPath,\n envKey,\n additionalEnvKeys,\n };\n }\n }\n\n return {\n id: id.toLowerCase(),\n label,\n aliases: Array.isArray(obj.aliases)\n ? obj.aliases\n .filter((v): v is string => typeof v === \"string\")\n .map((v) => v.toLowerCase())\n : [id.toLowerCase()],\n authorizationUrl,\n tokenUrl,\n scopes: Array.isArray(obj.scopes)\n ? obj.scopes.filter((v): v is string => typeof v === \"string\")\n : [],\n clientIdEnvKey,\n clientSecretEnvKey,\n accessTokenEnvKey,\n additionalAccessTokenEnvKeys: Array.isArray(obj.additionalAccessTokenEnvKeys)\n ? obj.additionalAccessTokenEnvKeys.filter((v): v is string => typeof v === \"string\")\n : undefined,\n refreshTokenEnvKey:\n typeof obj.refreshTokenEnvKey === \"string\" ? obj.refreshTokenEnvKey.trim() : undefined,\n authorizationParams: isRecord(obj.authorizationParams)\n ? Object.fromEntries(\n Object.entries(obj.authorizationParams).filter(\n (authorizationEntry): authorizationEntry is [string, string] =>\n typeof authorizationEntry[1] === \"string\",\n ),\n )\n : undefined,\n fileOutput,\n };\n })\n .filter((service): service is OAuthService => service !== null);\n\n const byId = new Map<string, OAuthService>();\n for (const service of builtins) byId.set(service.id, service);\n for (const service of custom) byId.set(service.id, service);\n return [...byId.values()];\n } catch (err) {\n log.logWarning(\n \"Failed to apply OAUTH_SERVICES_JSON overrides; using builtin OAuth services\",\n err instanceof Error ? err.message : String(err),\n );\n return builtins;\n }\n}\n\nexport function resolveOAuthService(input: string): OAuthService | undefined {\n const normalized = input.trim().toLowerCase();\n if (!normalized) return undefined;\n return getOAuthServices().find(\n (service) => service.id === normalized || service.aliases.includes(normalized),\n );\n}\n\nconst LOGIN_COMMANDS = [\"login\", \"/login\", \"/pi-login\"] as const;\n\nexport function parseLoginCommand(text: string): ParsedLoginCommand | null {\n const matched = matchCommand(text, LOGIN_COMMANDS);\n if (!matched) return null;\n\n const [subcommand, operation, name, ...extra] = matched.args;\n\n if (!subcommand) return { command: matched.command, action: \"setup\" };\n\n if (subcommand.toLowerCase() === \"shared\") {\n const op = operation?.toLowerCase();\n if (op === \"list\" && !name && extra.length === 0) {\n return { command: matched.command, action: \"shared_list\" };\n }\n if ((op === \"create\" || op === \"update\" || op === \"delete\") && !!name && extra.length === 0) {\n return {\n command: matched.command,\n action: `shared_${op}` as \"shared_create\" | \"shared_update\" | \"shared_delete\",\n name,\n };\n }\n return null;\n }\n\n if (subcommand.toLowerCase() === \"copy\" && operation && !name && extra.length === 0) {\n return { command: matched.command, action: \"copy_shared\", name: operation };\n }\n\n // Backward-compatible: older `/pi-login gh` / `/pi-login gws` forms opened the\n // generic login page and let the portal handle provider choice.\n if (!operation && extra.length === 0) return { command: matched.command, action: \"setup\" };\n\n return null;\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Server } from "http";
|
|
2
|
+
import { type SessionViewInteractiveOptions } from "../session-view/portal.js";
|
|
3
|
+
import type { InMemorySessionViewTokenStore } from "../session-view/store.js";
|
|
4
|
+
import type { InMemoryLinkTokenStore } from "./session.js";
|
|
5
|
+
import { type VaultManager } from "../vault.js";
|
|
6
|
+
/** Called after a binding is written, to notify the user in chat */
|
|
7
|
+
export type NotifyFn = (platform: string, conversationId: string, message: string) => Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Start a small HTTP server that receives credential onboarding callbacks from the web portal.
|
|
10
|
+
*
|
|
11
|
+
* Routes:
|
|
12
|
+
* GET /health — health check
|
|
13
|
+
* GET /link?token=xxx — credential onboarding page
|
|
14
|
+
* POST /api/link/complete — API key completion endpoint
|
|
15
|
+
* POST /api/oauth/start — creates provider OAuth redirect URL
|
|
16
|
+
* GET /oauth/callback — OAuth callback endpoint
|
|
17
|
+
*/
|
|
18
|
+
export declare function startLinkServer(port: number, linkTokenStore: InMemoryLinkTokenStore, vaultManager: VaultManager, notify: NotifyFn, sessionViewTokenStore?: InMemorySessionViewTokenStore, sessionViewInteractive?: SessionViewInteractiveOptions): Server;
|
|
19
|
+
//# sourceMappingURL=portal.d.ts.map
|