@imisbahk/hive 0.1.0 → 0.1.2
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/.github/workflows/publish.yml +31 -0
- package/.rocket/README.md +8 -8
- package/.rocket/SYMBOLS.md +260 -117
- package/Aborted +0 -0
- package/CONTRIBUTING.md +2 -1
- package/FEATURES.md +55 -0
- package/README.md +13 -11
- package/bun.lock +554 -0
- package/dist/agent/agent.d.ts +10 -1
- package/dist/agent/agent.d.ts.map +1 -1
- package/dist/agent/agent.js +351 -1
- package/dist/agent/agent.js.map +1 -1
- package/dist/browser/browser.d.ts +9 -0
- package/dist/browser/browser.d.ts.map +1 -0
- package/dist/browser/browser.js +338 -0
- package/dist/browser/browser.js.map +1 -0
- package/dist/cli/commands/chat.d.ts +5 -1
- package/dist/cli/commands/chat.d.ts.map +1 -1
- package/dist/cli/commands/chat.js +580 -38
- package/dist/cli/commands/chat.js.map +1 -1
- package/dist/cli/commands/config.d.ts +13 -0
- package/dist/cli/commands/config.d.ts.map +1 -1
- package/dist/cli/commands/config.js +257 -16
- package/dist/cli/commands/config.js.map +1 -1
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +39 -14
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/nuke.d.ts.map +1 -1
- package/dist/cli/commands/nuke.js +5 -4
- package/dist/cli/commands/nuke.js.map +1 -1
- package/dist/cli/commands/status.d.ts +5 -0
- package/dist/cli/commands/status.d.ts.map +1 -1
- package/dist/cli/commands/status.js +16 -6
- package/dist/cli/commands/status.js.map +1 -1
- package/dist/cli/index.js +34 -12
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/theme.d.ts +22 -0
- package/dist/cli/theme.d.ts.map +1 -0
- package/dist/cli/theme.js +63 -0
- package/dist/cli/theme.js.map +1 -0
- package/dist/cli/ui.d.ts +7 -0
- package/dist/cli/ui.d.ts.map +1 -0
- package/dist/cli/ui.js +101 -0
- package/dist/cli/ui.js.map +1 -0
- package/dist/providers/base.d.ts +37 -1
- package/dist/providers/base.d.ts.map +1 -1
- package/dist/providers/base.js +104 -0
- package/dist/providers/base.js.map +1 -1
- package/dist/providers/openai-compatible.d.ts +2 -1
- package/dist/providers/openai-compatible.d.ts.map +1 -1
- package/dist/providers/openai-compatible.js +18 -1
- package/dist/providers/openai-compatible.js.map +1 -1
- package/package.json +9 -1
- package/prompts/Browser.md +13 -0
- package/prompts/Debugging.md +15 -0
- package/prompts/Execution.md +13 -0
- package/prompts/Planning.md +13 -0
- package/prompts/Product.md +14 -0
- package/prompts/Review.md +15 -0
- package/prompts/Safety.md +12 -0
- package/prompts/Search.md +14 -0
- package/prompts/Tools.md +14 -0
- package/prompts/Writing.md +13 -0
- package/releases/v1/v0.1/RELEASE-NOTES.md +46 -0
- package/src/agent/agent.ts +442 -2
- package/src/browser/browser.ts +410 -0
- package/src/cli/commands/chat.ts +729 -34
- package/src/cli/commands/config.ts +344 -16
- package/src/cli/commands/init.ts +60 -14
- package/src/cli/commands/nuke.ts +11 -7
- package/src/cli/commands/status.ts +29 -6
- package/src/cli/index.ts +37 -9
- package/src/cli/theme.ts +88 -0
- package/src/cli/ui.ts +127 -0
- package/src/providers/base.ts +176 -1
- package/src/providers/openai-compatible.ts +24 -0
|
@@ -5,6 +5,12 @@ import { join } from "node:path";
|
|
|
5
5
|
import chalk from "chalk";
|
|
6
6
|
import { Command } from "commander";
|
|
7
7
|
import keytar from "keytar";
|
|
8
|
+
import {
|
|
9
|
+
renderHiveHeader,
|
|
10
|
+
renderInfo,
|
|
11
|
+
renderSeparator,
|
|
12
|
+
renderStep,
|
|
13
|
+
} from "../ui.js";
|
|
8
14
|
|
|
9
15
|
import {
|
|
10
16
|
closeHiveDatabase,
|
|
@@ -18,6 +24,10 @@ import {
|
|
|
18
24
|
const KEYCHAIN_SERVICE = "hive";
|
|
19
25
|
const PROMPTS_DIRECTORY = "prompts";
|
|
20
26
|
|
|
27
|
+
interface StatusCommandRenderOptions {
|
|
28
|
+
showHeader?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
21
31
|
export function registerStatusCommand(program: Command): void {
|
|
22
32
|
program
|
|
23
33
|
.command("status")
|
|
@@ -28,12 +38,23 @@ export function registerStatusCommand(program: Command): void {
|
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
export async function runStatusCommand(): Promise<void> {
|
|
41
|
+
await runStatusCommandWithOptions();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function runStatusCommandWithOptions(
|
|
45
|
+
options: StatusCommandRenderOptions = {},
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
const showHeader = options.showHeader ?? true;
|
|
48
|
+
if (showHeader) {
|
|
49
|
+
renderHiveHeader("Status");
|
|
50
|
+
}
|
|
51
|
+
|
|
31
52
|
const db = openHiveDatabase();
|
|
32
53
|
|
|
33
54
|
try {
|
|
34
55
|
const agent = getPrimaryAgent(db);
|
|
35
56
|
if (!agent) {
|
|
36
|
-
|
|
57
|
+
renderInfo("Hive is not initialized. Run `hive init` to get started.");
|
|
37
58
|
return;
|
|
38
59
|
}
|
|
39
60
|
|
|
@@ -45,14 +66,16 @@ export async function runStatusCommand(): Promise<void> {
|
|
|
45
66
|
const promptFiles = countFiles(promptsPath);
|
|
46
67
|
const initializedRaw = getMetaValue(db, "initialized_at") ?? agent.created_at;
|
|
47
68
|
|
|
48
|
-
|
|
49
|
-
|
|
69
|
+
if (showHeader) {
|
|
70
|
+
renderStep("Status");
|
|
71
|
+
}
|
|
72
|
+
renderSeparator();
|
|
50
73
|
printStatusLine("Agent", agent.agent_name ?? "not set");
|
|
51
74
|
printStatusLine("Owner", agent.name);
|
|
52
75
|
printStatusLine("Provider", provider);
|
|
53
76
|
printStatusLine("Model", agent.model);
|
|
54
77
|
printStatusLine("API Key", keyStatus);
|
|
55
|
-
|
|
78
|
+
renderSeparator();
|
|
56
79
|
printStatusLine(
|
|
57
80
|
"Database",
|
|
58
81
|
`${displayPath(dbPath)} (${formatBytes(dbSizeBytes)})`,
|
|
@@ -61,7 +84,7 @@ export async function runStatusCommand(): Promise<void> {
|
|
|
61
84
|
"Prompts",
|
|
62
85
|
`${ensureTrailingSlash(displayPath(promptsPath))} (${promptFiles} files)`,
|
|
63
86
|
);
|
|
64
|
-
|
|
87
|
+
renderSeparator();
|
|
65
88
|
printStatusLine("Initialized", formatDate(initializedRaw));
|
|
66
89
|
} finally {
|
|
67
90
|
closeHiveDatabase(db);
|
|
@@ -82,7 +105,7 @@ async function getApiKeyStatus(providerName: string): Promise<string> {
|
|
|
82
105
|
|
|
83
106
|
function printStatusLine(label: string, value: string): void {
|
|
84
107
|
const paddedLabel = `${label}:`.padEnd(10, " ");
|
|
85
|
-
console.log(`${chalk.dim(paddedLabel)} ${
|
|
108
|
+
console.log(`${chalk.dim(paddedLabel)} ${value}`);
|
|
86
109
|
}
|
|
87
110
|
|
|
88
111
|
function getFileSize(path: string): number {
|
package/src/cli/index.ts
CHANGED
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
import "dotenv/config";
|
|
4
4
|
|
|
5
|
-
import chalk from "chalk";
|
|
6
5
|
import { Command } from "commander";
|
|
7
6
|
|
|
8
|
-
import { registerChatCommand } from "./commands/chat.js";
|
|
7
|
+
import { registerChatCommand, runChatCommand } from "./commands/chat.js";
|
|
9
8
|
import { registerConfigCommand } from "./commands/config.js";
|
|
10
9
|
import { registerInitCommand } from "./commands/init.js";
|
|
11
10
|
import { registerNukeCommand } from "./commands/nuke.js";
|
|
12
11
|
import { registerStatusCommand } from "./commands/status.js";
|
|
12
|
+
import { renderError, renderHiveHeader } from "./ui.js";
|
|
13
13
|
|
|
14
14
|
const program = new Command();
|
|
15
15
|
|
|
16
16
|
program
|
|
17
17
|
.name("hive")
|
|
18
18
|
.description("Your agent. Always running. Always learning. Always working.")
|
|
19
|
-
.version("0.1.
|
|
19
|
+
.version("0.1.1");
|
|
20
20
|
|
|
21
21
|
registerInitCommand(program);
|
|
22
22
|
registerChatCommand(program);
|
|
@@ -24,15 +24,43 @@ registerConfigCommand(program);
|
|
|
24
24
|
registerStatusCommand(program);
|
|
25
25
|
registerNukeCommand(program);
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const argv = process.argv.slice(2);
|
|
28
|
+
|
|
29
|
+
void main();
|
|
30
|
+
|
|
31
|
+
async function main(): Promise<void> {
|
|
32
|
+
try {
|
|
33
|
+
if (argv.length === 0) {
|
|
34
|
+
await runChatCommand({}, { entrypoint: "default" });
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (shouldRenderHelpHeader(argv)) {
|
|
39
|
+
renderHiveHeader(resolveHelpTitle(argv));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
await program.parseAsync(process.argv);
|
|
43
|
+
} catch (error) {
|
|
30
44
|
if (error instanceof Error) {
|
|
31
|
-
|
|
45
|
+
renderError(error.message);
|
|
32
46
|
process.exitCode = 1;
|
|
33
47
|
return;
|
|
34
48
|
}
|
|
35
49
|
|
|
36
|
-
|
|
50
|
+
renderError(String(error));
|
|
37
51
|
process.exitCode = 1;
|
|
38
|
-
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function shouldRenderHelpHeader(args: string[]): boolean {
|
|
56
|
+
return args[0] === "help" || args.includes("-h") || args.includes("--help");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function resolveHelpTitle(args: string[]): string {
|
|
60
|
+
if (args[0] === "help") {
|
|
61
|
+
return args[1] ?? "Help";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const commandName = args.find((arg) => !arg.startsWith("-"));
|
|
65
|
+
return commandName ?? "Help";
|
|
66
|
+
}
|
package/src/cli/theme.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
}
|
package/src/providers/base.ts
CHANGED
|
@@ -12,11 +12,38 @@ export const SUPPORTED_PROVIDER_NAMES = [
|
|
|
12
12
|
] as const;
|
|
13
13
|
|
|
14
14
|
export type ProviderName = (typeof SUPPORTED_PROVIDER_NAMES)[number];
|
|
15
|
-
export type ProviderMessageRole = "system" | "user" | "assistant";
|
|
15
|
+
export type ProviderMessageRole = "system" | "user" | "assistant" | "tool";
|
|
16
|
+
|
|
17
|
+
export interface ProviderToolDefinition {
|
|
18
|
+
type: "function";
|
|
19
|
+
function: {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
parameters: Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ProviderToolCallPayload {
|
|
27
|
+
id: string;
|
|
28
|
+
type: "function";
|
|
29
|
+
function: {
|
|
30
|
+
name: string;
|
|
31
|
+
arguments: string;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ProviderToolCall {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
arguments: string;
|
|
39
|
+
}
|
|
16
40
|
|
|
17
41
|
export interface ProviderMessage {
|
|
18
42
|
role: ProviderMessageRole;
|
|
19
43
|
content: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
tool_call_id?: string;
|
|
46
|
+
tool_calls?: ProviderToolCallPayload[];
|
|
20
47
|
}
|
|
21
48
|
|
|
22
49
|
export interface StreamChatRequest {
|
|
@@ -26,10 +53,20 @@ export interface StreamChatRequest {
|
|
|
26
53
|
maxTokens?: number;
|
|
27
54
|
}
|
|
28
55
|
|
|
56
|
+
export interface CompleteChatRequest extends StreamChatRequest {
|
|
57
|
+
tools?: ProviderToolDefinition[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface CompleteChatResponse {
|
|
61
|
+
content: string;
|
|
62
|
+
toolCalls: ProviderToolCall[];
|
|
63
|
+
}
|
|
64
|
+
|
|
29
65
|
export interface Provider {
|
|
30
66
|
readonly name: ProviderName;
|
|
31
67
|
readonly defaultModel: string;
|
|
32
68
|
streamChat(request: StreamChatRequest): AsyncGenerator<string>;
|
|
69
|
+
completeChat?(request: CompleteChatRequest): Promise<CompleteChatResponse>;
|
|
33
70
|
}
|
|
34
71
|
|
|
35
72
|
export class ProviderConfigurationError extends Error {
|
|
@@ -58,6 +95,10 @@ export interface OpenAICompatibleStreamInput {
|
|
|
58
95
|
extraBody?: Record<string, unknown>;
|
|
59
96
|
}
|
|
60
97
|
|
|
98
|
+
export interface OpenAICompatibleCompleteInput extends OpenAICompatibleStreamInput {
|
|
99
|
+
tools?: ProviderToolDefinition[];
|
|
100
|
+
}
|
|
101
|
+
|
|
61
102
|
export function normalizeProviderName(raw?: string): ProviderName {
|
|
62
103
|
if (!raw) {
|
|
63
104
|
return "openai";
|
|
@@ -146,6 +187,71 @@ export async function* streamOpenAICompatibleChat(
|
|
|
146
187
|
}
|
|
147
188
|
}
|
|
148
189
|
|
|
190
|
+
export async function completeOpenAICompatibleChat(
|
|
191
|
+
input: OpenAICompatibleCompleteInput,
|
|
192
|
+
): Promise<CompleteChatResponse> {
|
|
193
|
+
const endpoint = `${input.baseUrl.replace(/\/$/, "")}/chat/completions`;
|
|
194
|
+
|
|
195
|
+
const headers: Record<string, string> = {
|
|
196
|
+
"content-type": "application/json",
|
|
197
|
+
...(input.extraHeaders ?? {}),
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
if (input.apiKey) {
|
|
201
|
+
headers.authorization = `Bearer ${input.apiKey}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const body: Record<string, unknown> = {
|
|
205
|
+
model: input.model,
|
|
206
|
+
messages: input.messages,
|
|
207
|
+
stream: false,
|
|
208
|
+
...(input.extraBody ?? {}),
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
if (input.temperature !== undefined) {
|
|
212
|
+
body.temperature = input.temperature;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (input.maxTokens !== undefined) {
|
|
216
|
+
body.max_tokens = input.maxTokens;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (input.tools && input.tools.length > 0) {
|
|
220
|
+
body.tools = input.tools;
|
|
221
|
+
body.tool_choice = "auto";
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const response = await fetch(endpoint, {
|
|
225
|
+
method: "POST",
|
|
226
|
+
headers,
|
|
227
|
+
body: JSON.stringify(body),
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
await ensureOk(response, `${input.provider} request failed`);
|
|
231
|
+
|
|
232
|
+
const payload = (await response.json()) as Record<string, unknown>;
|
|
233
|
+
const errorMessage = pickErrorMessage(payload);
|
|
234
|
+
if (errorMessage) {
|
|
235
|
+
throw new ProviderRequestError(`${input.provider} error: ${errorMessage}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const maybeChoices = payload.choices;
|
|
239
|
+
if (!Array.isArray(maybeChoices) || maybeChoices.length === 0) {
|
|
240
|
+
throw new ProviderRequestError(`${input.provider} response did not include choices.`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const firstChoice = maybeChoices[0] as Record<string, unknown>;
|
|
244
|
+
const message = firstChoice.message as Record<string, unknown> | undefined;
|
|
245
|
+
|
|
246
|
+
const content = pickMessageContent(message);
|
|
247
|
+
const toolCalls = pickToolCalls(message);
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
content,
|
|
251
|
+
toolCalls,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
149
255
|
export async function* iterateSseData(response: Response): AsyncGenerator<string> {
|
|
150
256
|
if (!response.body) {
|
|
151
257
|
return;
|
|
@@ -220,6 +326,75 @@ function pickErrorMessage(payload: Record<string, unknown>): string | null {
|
|
|
220
326
|
return null;
|
|
221
327
|
}
|
|
222
328
|
|
|
329
|
+
function pickMessageContent(message: Record<string, unknown> | undefined): string {
|
|
330
|
+
if (!message) {
|
|
331
|
+
return "";
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const rawContent = message.content;
|
|
335
|
+
if (typeof rawContent === "string") {
|
|
336
|
+
return rawContent;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (Array.isArray(rawContent)) {
|
|
340
|
+
return rawContent
|
|
341
|
+
.map((part) => {
|
|
342
|
+
if (typeof part === "string") {
|
|
343
|
+
return part;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (part && typeof part === "object") {
|
|
347
|
+
const asRecord = part as Record<string, unknown>;
|
|
348
|
+
if (typeof asRecord.text === "string") {
|
|
349
|
+
return asRecord.text;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return "";
|
|
354
|
+
})
|
|
355
|
+
.join("");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return "";
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function pickToolCalls(message: Record<string, unknown> | undefined): ProviderToolCall[] {
|
|
362
|
+
if (!message) {
|
|
363
|
+
return [];
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const rawToolCalls = message.tool_calls;
|
|
367
|
+
if (!Array.isArray(rawToolCalls)) {
|
|
368
|
+
return [];
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const calls: ProviderToolCall[] = [];
|
|
372
|
+
for (const toolCall of rawToolCalls) {
|
|
373
|
+
if (!toolCall || typeof toolCall !== "object") {
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const callRecord = toolCall as Record<string, unknown>;
|
|
378
|
+
const callId = typeof callRecord.id === "string" ? callRecord.id : "";
|
|
379
|
+
const callFunction = callRecord.function as Record<string, unknown> | undefined;
|
|
380
|
+
const callName = typeof callFunction?.name === "string" ? callFunction.name : "";
|
|
381
|
+
const callArguments =
|
|
382
|
+
typeof callFunction?.arguments === "string" ? callFunction.arguments : "{}";
|
|
383
|
+
|
|
384
|
+
if (callId.length === 0 || callName.length === 0) {
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
calls.push({
|
|
389
|
+
id: callId,
|
|
390
|
+
name: callName,
|
|
391
|
+
arguments: callArguments,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return calls;
|
|
396
|
+
}
|
|
397
|
+
|
|
223
398
|
async function ensureOk(response: Response, fallbackMessage: string): Promise<void> {
|
|
224
399
|
if (response.ok) {
|
|
225
400
|
return;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
+
completeOpenAICompatibleChat,
|
|
3
|
+
type CompleteChatRequest,
|
|
4
|
+
type CompleteChatResponse,
|
|
2
5
|
ProviderConfigurationError,
|
|
3
6
|
type Provider,
|
|
4
7
|
type ProviderName,
|
|
@@ -55,4 +58,25 @@ export class OpenAICompatibleProvider implements Provider {
|
|
|
55
58
|
extraBody: this.extraBody,
|
|
56
59
|
});
|
|
57
60
|
}
|
|
61
|
+
|
|
62
|
+
async completeChat(request: CompleteChatRequest): Promise<CompleteChatResponse> {
|
|
63
|
+
if (!this.allowMissingApiKey && !this.apiKey) {
|
|
64
|
+
throw new ProviderConfigurationError(
|
|
65
|
+
`Provider \"${this.name}\" is missing an API key.`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return completeOpenAICompatibleChat({
|
|
70
|
+
provider: this.name,
|
|
71
|
+
baseUrl: this.baseUrl,
|
|
72
|
+
apiKey: this.apiKey,
|
|
73
|
+
model: request.model ?? this.defaultModel,
|
|
74
|
+
messages: request.messages,
|
|
75
|
+
temperature: request.temperature,
|
|
76
|
+
maxTokens: request.maxTokens,
|
|
77
|
+
tools: request.tools,
|
|
78
|
+
extraHeaders: this.extraHeaders,
|
|
79
|
+
extraBody: this.extraBody,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
58
82
|
}
|