@imisbahk/hive 0.1.3 → 0.1.6
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/.gitattributes +7 -0
- package/.rocket/README.md +22 -22
- package/dist/agent/agent.d.ts +7 -1
- package/dist/agent/agent.d.ts.map +1 -1
- package/dist/agent/agent.js +139 -27
- package/dist/agent/agent.js.map +1 -1
- package/dist/agent/hive-ctx.d.ts +23 -0
- package/dist/agent/hive-ctx.d.ts.map +1 -0
- package/dist/agent/hive-ctx.js +68 -0
- package/dist/agent/hive-ctx.js.map +1 -0
- package/dist/agent/prompts.d.ts +15 -0
- package/dist/agent/prompts.d.ts.map +1 -0
- package/dist/agent/prompts.js +153 -0
- package/dist/agent/prompts.js.map +1 -0
- package/dist/cli/commands/chat.d.ts.map +1 -1
- package/dist/cli/commands/chat.js +788 -29
- package/dist/cli/commands/chat.js.map +1 -1
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +93 -27
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/memory.d.ts +3 -0
- package/dist/cli/commands/memory.d.ts.map +1 -0
- package/dist/cli/commands/memory.js +104 -0
- package/dist/cli/commands/memory.js.map +1 -0
- package/dist/cli/index.js +3 -1
- package/dist/cli/index.js.map +1 -1
- package/dist/providers/anthropic.d.ts +1 -0
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +1 -0
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/api-key.js +1 -1
- package/dist/providers/api-key.js.map +1 -1
- package/dist/providers/base.d.ts +1 -0
- package/dist/providers/base.d.ts.map +1 -1
- package/dist/providers/base.js +1 -1
- package/dist/providers/base.js.map +1 -1
- package/dist/providers/groq.d.ts.map +1 -1
- package/dist/providers/groq.js +1 -0
- package/dist/providers/groq.js.map +1 -1
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +41 -8
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +1 -0
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai-compatible.d.ts +2 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -1
- package/dist/providers/openai-compatible.js +5 -3
- package/dist/providers/openai-compatible.js.map +1 -1
- package/dist/providers/resilience.d.ts +4 -0
- package/dist/providers/resilience.d.ts.map +1 -0
- package/dist/providers/resilience.js +38 -0
- package/dist/providers/resilience.js.map +1 -0
- package/dist/storage/db.d.ts +31 -2
- package/dist/storage/db.d.ts.map +1 -1
- package/dist/storage/db.js +165 -3
- package/dist/storage/db.js.map +1 -1
- package/dist/storage/schema.d.ts +12 -1
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +29 -1
- package/dist/storage/schema.js.map +1 -1
- package/master +0 -0
- package/package.json +9 -2
- package/.github/workflows/publish.yml +0 -31
- package/.rocket/ARCHITECTURE.md +0 -7
- package/.rocket/SYMBOLS.md +0 -425
- package/001-local-first-storage.md +0 -43
- package/003-memory-architechture.md +0 -71
- package/CONTRIBUTING.md +0 -150
- package/FEATURES.md +0 -63
- package/index.md +0 -16
- package/prompts/Behaviour.md +0 -23
- package/prompts/Browser.md +0 -13
- package/prompts/Code.md +0 -12
- package/prompts/Debugging.md +0 -15
- package/prompts/Execution.md +0 -13
- package/prompts/Memory.md +0 -11
- package/prompts/Planning.md +0 -13
- package/prompts/Product.md +0 -14
- package/prompts/Review.md +0 -15
- package/prompts/Safety.md +0 -12
- package/prompts/Search.md +0 -14
- package/prompts/System.md +0 -6
- package/prompts/Tools.md +0 -14
- package/prompts/Writing.md +0 -13
- package/releases/v1/v0.1/RELEASE-NOTES.md +0 -101
- package/src/agent/agent.ts +0 -595
- package/src/agent/index.ts +0 -2
- package/src/browser/browser.ts +0 -410
- package/src/cli/commands/chat.ts +0 -864
- package/src/cli/commands/config.ts +0 -610
- package/src/cli/commands/doctor.ts +0 -655
- package/src/cli/commands/init.ts +0 -288
- package/src/cli/commands/nuke.ts +0 -64
- package/src/cli/commands/status.ts +0 -170
- package/src/cli/helpers/providerPrompts.ts +0 -192
- package/src/cli/index.ts +0 -68
- package/src/cli/theme.ts +0 -88
- package/src/cli/ui.ts +0 -127
- package/src/providers/anthropic.ts +0 -146
- package/src/providers/api-key.ts +0 -23
- package/src/providers/base.ts +0 -409
- package/src/providers/google.ts +0 -21
- package/src/providers/groq.ts +0 -21
- package/src/providers/index.ts +0 -65
- package/src/providers/mistral.ts +0 -21
- package/src/providers/ollama.ts +0 -22
- package/src/providers/openai-compatible.ts +0 -82
- package/src/providers/openai.ts +0 -21
- package/src/providers/openrouter.ts +0 -21
- package/src/providers/together.ts +0 -21
- package/src/storage/db.ts +0 -476
- package/src/storage/schema.ts +0 -116
- package/tsconfig.json +0 -51
package/src/cli/index.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import "dotenv/config";
|
|
4
|
-
|
|
5
|
-
import { Command } from "commander";
|
|
6
|
-
|
|
7
|
-
import { registerChatCommand, runChatCommand } from "./commands/chat.js";
|
|
8
|
-
import { registerConfigCommand } from "./commands/config.js";
|
|
9
|
-
import { registerDoctorCommand } from "./commands/doctor.js";
|
|
10
|
-
import { registerInitCommand } from "./commands/init.js";
|
|
11
|
-
import { registerNukeCommand } from "./commands/nuke.js";
|
|
12
|
-
import { registerStatusCommand } from "./commands/status.js";
|
|
13
|
-
import { renderError, renderHiveHeader } from "./ui.js";
|
|
14
|
-
|
|
15
|
-
const program = new Command();
|
|
16
|
-
|
|
17
|
-
program
|
|
18
|
-
.name("hive")
|
|
19
|
-
.description("Your agent. Always running. Always learning. Always working.")
|
|
20
|
-
.version("0.1.1");
|
|
21
|
-
|
|
22
|
-
registerInitCommand(program);
|
|
23
|
-
registerChatCommand(program);
|
|
24
|
-
registerConfigCommand(program);
|
|
25
|
-
registerDoctorCommand(program);
|
|
26
|
-
registerStatusCommand(program);
|
|
27
|
-
registerNukeCommand(program);
|
|
28
|
-
|
|
29
|
-
const argv = process.argv.slice(2);
|
|
30
|
-
|
|
31
|
-
void main();
|
|
32
|
-
|
|
33
|
-
async function main(): Promise<void> {
|
|
34
|
-
try {
|
|
35
|
-
if (argv.length === 0) {
|
|
36
|
-
await runChatCommand({}, { entrypoint: "default" });
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (shouldRenderHelpHeader(argv)) {
|
|
41
|
-
renderHiveHeader(resolveHelpTitle(argv));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
await program.parseAsync(process.argv);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
if (error instanceof Error) {
|
|
47
|
-
renderError(error.message);
|
|
48
|
-
process.exitCode = 1;
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
renderError(String(error));
|
|
53
|
-
process.exitCode = 1;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function shouldRenderHelpHeader(args: string[]): boolean {
|
|
58
|
-
return args[0] === "help" || args.includes("-h") || args.includes("--help");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function resolveHelpTitle(args: string[]): string {
|
|
62
|
-
if (args[0] === "help") {
|
|
63
|
-
return args[1] ?? "Help";
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const commandName = args.find((arg) => !arg.startsWith("-"));
|
|
67
|
-
return commandName ?? "Help";
|
|
68
|
-
}
|
package/src/cli/theme.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import chalk, { type ChalkInstance } from "chalk";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
closeHiveDatabase,
|
|
5
|
-
getMetaValue,
|
|
6
|
-
openHiveDatabase,
|
|
7
|
-
} from "../storage/db.js";
|
|
8
|
-
|
|
9
|
-
export const DEFAULT_THEME_NAME = "amber";
|
|
10
|
-
export const DEFAULT_THEME_HEX = "#FFA500";
|
|
11
|
-
|
|
12
|
-
export const BUILT_IN_THEMES = {
|
|
13
|
-
amber: "#FFA500",
|
|
14
|
-
cyan: "#00BCD4",
|
|
15
|
-
rose: "#FF4081",
|
|
16
|
-
slate: "#90A4AE",
|
|
17
|
-
green: "#00E676",
|
|
18
|
-
} as const;
|
|
19
|
-
|
|
20
|
-
export const HEX_COLOR_PATTERN = /^#[0-9A-Fa-f]{6}$/;
|
|
21
|
-
|
|
22
|
-
export type BuiltInThemeName = keyof typeof BUILT_IN_THEMES;
|
|
23
|
-
export type ThemeName = BuiltInThemeName | "custom";
|
|
24
|
-
|
|
25
|
-
export interface HiveTheme {
|
|
26
|
-
name: ThemeName;
|
|
27
|
-
hex: string;
|
|
28
|
-
accent: ChalkInstance;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function applyTheme(hex: string): ChalkInstance {
|
|
32
|
-
const normalizedHex = normalizeHex(hex);
|
|
33
|
-
return chalk.hex(normalizedHex);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function getTheme(): HiveTheme {
|
|
37
|
-
let db: ReturnType<typeof openHiveDatabase> | null = null;
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
db = openHiveDatabase();
|
|
41
|
-
const storedName = getMetaValue(db, "theme");
|
|
42
|
-
const storedHex = getMetaValue(db, "theme_hex");
|
|
43
|
-
return resolveTheme(storedName, storedHex);
|
|
44
|
-
} catch {
|
|
45
|
-
return makeTheme(DEFAULT_THEME_NAME, DEFAULT_THEME_HEX);
|
|
46
|
-
} finally {
|
|
47
|
-
if (db) {
|
|
48
|
-
closeHiveDatabase(db);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function isValidHexColor(value: string): boolean {
|
|
54
|
-
return HEX_COLOR_PATTERN.test(value);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function resolveTheme(storedName: string | null, storedHex: string | null): HiveTheme {
|
|
58
|
-
if (isBuiltInTheme(storedName)) {
|
|
59
|
-
return makeTheme(storedName, BUILT_IN_THEMES[storedName]);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (storedName === "custom" && storedHex && isValidHexColor(storedHex)) {
|
|
63
|
-
return makeTheme("custom", storedHex);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return makeTheme(DEFAULT_THEME_NAME, DEFAULT_THEME_HEX);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function makeTheme(name: ThemeName, hex: string): HiveTheme {
|
|
70
|
-
const normalizedHex = normalizeHex(hex);
|
|
71
|
-
return {
|
|
72
|
-
name,
|
|
73
|
-
hex: normalizedHex,
|
|
74
|
-
accent: applyTheme(normalizedHex),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function normalizeHex(value: string): string {
|
|
79
|
-
if (!isValidHexColor(value)) {
|
|
80
|
-
return DEFAULT_THEME_HEX;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return value.toUpperCase();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function isBuiltInTheme(value: string | null): value is BuiltInThemeName {
|
|
87
|
-
return value !== null && value in BUILT_IN_THEMES;
|
|
88
|
-
}
|
package/src/cli/ui.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import process from "node:process";
|
|
3
|
-
|
|
4
|
-
import chalk from "chalk";
|
|
5
|
-
|
|
6
|
-
import { getTheme } from "./theme.js";
|
|
7
|
-
|
|
8
|
-
const WORDMARK_LINES = [
|
|
9
|
-
" ██╗ ██╗██╗██╗ ██╗███████╗",
|
|
10
|
-
" ██║ ██║██║██║ ██║██╔════╝",
|
|
11
|
-
" ███████║██║██║ ██║█████╗ ",
|
|
12
|
-
" ██╔══██║██║╚██╗ ██╔╝██╔══╝ ",
|
|
13
|
-
" ██║ ██║██║ ╚████╔╝ ███████╗",
|
|
14
|
-
" ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚══════╝",
|
|
15
|
-
] as const;
|
|
16
|
-
|
|
17
|
-
const COMMAND_CENTRE_LABEL = "COMMAND CENTRE";
|
|
18
|
-
const MAX_SEPARATOR_WIDTH = 72;
|
|
19
|
-
const MIN_SEPARATOR_WIDTH = 24;
|
|
20
|
-
|
|
21
|
-
let cachedVersion: string | null = null;
|
|
22
|
-
|
|
23
|
-
export function renderHiveHeader(pageTitle?: string): void {
|
|
24
|
-
const terminalWidth = getTerminalWidth();
|
|
25
|
-
const separator = "─".repeat(getSeparatorWidth(terminalWidth));
|
|
26
|
-
const accent = getTheme().accent;
|
|
27
|
-
|
|
28
|
-
for (const line of WORDMARK_LINES) {
|
|
29
|
-
console.log(accent.bold(centerText(line, terminalWidth)));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
console.log("");
|
|
33
|
-
console.log(chalk.dim(centerText(`v${getCliVersion()}`, terminalWidth)));
|
|
34
|
-
|
|
35
|
-
const normalizedTitle = normalizePageTitle(pageTitle);
|
|
36
|
-
const commandCentreTitle = normalizedTitle
|
|
37
|
-
? `${COMMAND_CENTRE_LABEL} · ${normalizedTitle}`
|
|
38
|
-
: COMMAND_CENTRE_LABEL;
|
|
39
|
-
|
|
40
|
-
console.log(accent(centerText(commandCentreTitle, terminalWidth)));
|
|
41
|
-
console.log(accent(centerText(separator, terminalWidth)));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function renderSuccess(message: string): void {
|
|
45
|
-
const accent = getTheme().accent;
|
|
46
|
-
console.log(`${accent("✓")} ${message}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function renderError(message: string): void {
|
|
50
|
-
console.error(chalk.red(message));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function renderStep(message: string): void {
|
|
54
|
-
const accent = getTheme().accent;
|
|
55
|
-
console.log(`${accent("›")} ${message}`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function renderInfo(message: string): void {
|
|
59
|
-
console.log(chalk.dim(message));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function renderSeparator(text?: string): void {
|
|
63
|
-
const accent = getTheme().accent;
|
|
64
|
-
|
|
65
|
-
if (text) {
|
|
66
|
-
console.log(accent(text));
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
console.log(accent("─".repeat(getSeparatorWidth(getTerminalWidth()))));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function getCliVersion(): string {
|
|
74
|
-
if (cachedVersion) {
|
|
75
|
-
return cachedVersion;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const raw = readFileSync(new URL("../../package.json", import.meta.url), "utf8");
|
|
80
|
-
const parsed = JSON.parse(raw) as { version?: unknown };
|
|
81
|
-
if (typeof parsed.version === "string" && parsed.version.trim().length > 0) {
|
|
82
|
-
cachedVersion = parsed.version.trim();
|
|
83
|
-
return cachedVersion;
|
|
84
|
-
}
|
|
85
|
-
} catch {
|
|
86
|
-
// Fall through to default when package metadata cannot be read.
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
cachedVersion = "0.0.0";
|
|
90
|
-
return cachedVersion;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function normalizePageTitle(pageTitle?: string): string {
|
|
94
|
-
const trimmed = pageTitle?.trim() ?? "";
|
|
95
|
-
if (trimmed.length === 0) {
|
|
96
|
-
return "";
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return trimmed.toUpperCase();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function getTerminalWidth(): number {
|
|
103
|
-
if (!process.stdout.isTTY) {
|
|
104
|
-
return 80;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const columns = process.stdout.columns;
|
|
108
|
-
if (typeof columns !== "number" || columns < 20) {
|
|
109
|
-
return 80;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return columns;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function centerText(value: string, totalWidth: number): string {
|
|
116
|
-
if (value.length >= totalWidth) {
|
|
117
|
-
return value;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const leftPadding = Math.floor((totalWidth - value.length) / 2);
|
|
121
|
-
return `${" ".repeat(leftPadding)}${value}`;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function getSeparatorWidth(terminalWidth: number): number {
|
|
125
|
-
const usableWidth = Math.max(MIN_SEPARATOR_WIDTH, terminalWidth - 8);
|
|
126
|
-
return Math.min(MAX_SEPARATOR_WIDTH, usableWidth);
|
|
127
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import fetch from "node-fetch";
|
|
2
|
-
|
|
3
|
-
import { resolveProviderApiKey } from "./api-key.js";
|
|
4
|
-
import {
|
|
5
|
-
ProviderConfigurationError,
|
|
6
|
-
ProviderRequestError,
|
|
7
|
-
iterateSseData,
|
|
8
|
-
type Provider,
|
|
9
|
-
type ProviderMessage,
|
|
10
|
-
type StreamChatRequest,
|
|
11
|
-
} from "./base.js";
|
|
12
|
-
|
|
13
|
-
const ANTHROPIC_API_URL = "https://api.anthropic.com/v1/messages";
|
|
14
|
-
const DEFAULT_ANTHROPIC_MODEL = "claude-3-5-haiku-latest";
|
|
15
|
-
|
|
16
|
-
interface AnthropicMessage {
|
|
17
|
-
role: "user" | "assistant";
|
|
18
|
-
content: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class AnthropicProvider implements Provider {
|
|
22
|
-
readonly name = "anthropic" as const;
|
|
23
|
-
readonly defaultModel: string;
|
|
24
|
-
|
|
25
|
-
private readonly apiKey?: string;
|
|
26
|
-
|
|
27
|
-
constructor(apiKey?: string) {
|
|
28
|
-
this.apiKey = apiKey;
|
|
29
|
-
this.defaultModel = process.env.ANTHROPIC_MODEL ?? DEFAULT_ANTHROPIC_MODEL;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async *streamChat(request: StreamChatRequest): AsyncGenerator<string> {
|
|
33
|
-
if (!this.apiKey) {
|
|
34
|
-
throw new ProviderConfigurationError(
|
|
35
|
-
'Provider "anthropic" is missing ANTHROPIC_API_KEY.',
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const system = request.messages
|
|
40
|
-
.filter((message) => message.role === "system")
|
|
41
|
-
.map((message) => message.content)
|
|
42
|
-
.join("\n\n");
|
|
43
|
-
|
|
44
|
-
const messages = toAnthropicMessages(request.messages);
|
|
45
|
-
|
|
46
|
-
const response = await fetch(ANTHROPIC_API_URL, {
|
|
47
|
-
method: "POST",
|
|
48
|
-
headers: {
|
|
49
|
-
"content-type": "application/json",
|
|
50
|
-
"x-api-key": this.apiKey,
|
|
51
|
-
"anthropic-version": "2023-06-01",
|
|
52
|
-
},
|
|
53
|
-
body: JSON.stringify({
|
|
54
|
-
model: request.model ?? this.defaultModel,
|
|
55
|
-
stream: true,
|
|
56
|
-
max_tokens: request.maxTokens ?? 1024,
|
|
57
|
-
temperature: request.temperature,
|
|
58
|
-
system: system.length > 0 ? system : undefined,
|
|
59
|
-
messages,
|
|
60
|
-
}),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
const bodyText = await response.text();
|
|
65
|
-
throw new ProviderRequestError(
|
|
66
|
-
`anthropic request failed: HTTP ${response.status} ${response.statusText} ${bodyText}`,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
for await (const data of iterateSseData(response)) {
|
|
71
|
-
if (data === "[DONE]") {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const payload = parseJson<Record<string, unknown>>(data);
|
|
76
|
-
if (!payload) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (payload.type === "error") {
|
|
81
|
-
const error = payload.error as Record<string, unknown> | undefined;
|
|
82
|
-
const message =
|
|
83
|
-
typeof error?.message === "string"
|
|
84
|
-
? error.message
|
|
85
|
-
: "anthropic stream error";
|
|
86
|
-
|
|
87
|
-
throw new ProviderRequestError(message);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const text = pickAnthropicDelta(payload);
|
|
91
|
-
if (text.length > 0) {
|
|
92
|
-
yield text;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export async function createAnthropicProvider(): Promise<AnthropicProvider> {
|
|
99
|
-
const apiKey = await resolveProviderApiKey("anthropic", "ANTHROPIC_API_KEY");
|
|
100
|
-
return new AnthropicProvider(apiKey);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function toAnthropicMessages(messages: ProviderMessage[]): AnthropicMessage[] {
|
|
104
|
-
const filtered = messages.filter((message) => message.role !== "system");
|
|
105
|
-
if (filtered.length === 0) {
|
|
106
|
-
return [{ role: "user", content: "Hello." }];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return filtered.map((message) => ({
|
|
110
|
-
role: message.role === "assistant" ? "assistant" : "user",
|
|
111
|
-
content: message.content,
|
|
112
|
-
}));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function pickAnthropicDelta(payload: Record<string, unknown>): string {
|
|
116
|
-
if (payload.type === "content_block_start") {
|
|
117
|
-
const contentBlock = payload.content_block as Record<string, unknown> | undefined;
|
|
118
|
-
if (contentBlock?.type === "text" && typeof contentBlock.text === "string") {
|
|
119
|
-
return contentBlock.text;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (payload.type === "content_block_delta") {
|
|
124
|
-
const delta = payload.delta as Record<string, unknown> | undefined;
|
|
125
|
-
if (delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
126
|
-
return delta.text;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (payload.type === "message_delta") {
|
|
131
|
-
const delta = payload.delta as Record<string, unknown> | undefined;
|
|
132
|
-
if (typeof delta?.text === "string") {
|
|
133
|
-
return delta.text;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return "";
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function parseJson<T>(value: string): T | null {
|
|
141
|
-
try {
|
|
142
|
-
return JSON.parse(value) as T;
|
|
143
|
-
} catch {
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
}
|
package/src/providers/api-key.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import keytar from "keytar";
|
|
2
|
-
|
|
3
|
-
import type { ProviderName } from "./base.js";
|
|
4
|
-
|
|
5
|
-
const KEYCHAIN_SERVICE = "hive";
|
|
6
|
-
|
|
7
|
-
export async function resolveProviderApiKey(
|
|
8
|
-
providerName: ProviderName,
|
|
9
|
-
envVarName: string,
|
|
10
|
-
): Promise<string | undefined> {
|
|
11
|
-
let keychainValue: string | null = null;
|
|
12
|
-
try {
|
|
13
|
-
keychainValue = await keytar.getPassword(KEYCHAIN_SERVICE, providerName);
|
|
14
|
-
} catch {
|
|
15
|
-
keychainValue = null;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
keychainValue ??
|
|
20
|
-
process.env[envVarName] ??
|
|
21
|
-
undefined
|
|
22
|
-
);
|
|
23
|
-
}
|