@denizokcu/haze 0.8.0 → 0.10.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 +67 -17
- package/README.md +121 -64
- package/dist/cli/chat/chatMetrics.d.ts +17 -1
- package/dist/cli/chat/chatMetrics.js +22 -5
- package/dist/cli/chat/contextReport.d.ts +11 -0
- package/dist/cli/chat/contextReport.js +44 -0
- package/dist/cli/chat/fileMentionSuggestions.d.ts +29 -0
- package/dist/cli/chat/fileMentionSuggestions.js +91 -0
- package/dist/cli/chat/inputSuggestions.d.ts +5 -0
- package/dist/cli/chat/inputSuggestions.js +17 -3
- package/dist/cli/chat/sessionLifecycle.d.ts +58 -0
- package/dist/cli/chat/sessionLifecycle.js +155 -0
- package/dist/cli/chat/sessionRecorder.d.ts +3 -0
- package/dist/cli/chat/sessionRecorder.js +21 -8
- package/dist/cli/chat/startupInfo.d.ts +1 -0
- package/dist/cli/chat/startupInfo.js +11 -0
- package/dist/cli/chat/tips.d.ts +21 -0
- package/dist/cli/chat/tips.js +43 -0
- package/dist/cli/chat/wizardDispatch.d.ts +64 -0
- package/dist/cli/chat/wizardDispatch.js +645 -0
- package/dist/cli/commands/chat.d.ts +1 -0
- package/dist/cli/commands/chat.js +246 -779
- package/dist/cli/commands/chatModes.d.ts +1 -1
- package/dist/cli/commands/chatModes.js +7 -2
- package/dist/cli/commands/commandHelp.js +5 -3
- package/dist/cli/commands/commands.d.ts +4 -3
- package/dist/cli/commands/commands.js +12 -13
- package/dist/cli/commands/fleetCommand.d.ts +14 -0
- package/dist/cli/commands/fleetCommand.js +46 -0
- package/dist/cli/commands/formatters.d.ts +5 -0
- package/dist/cli/commands/formatters.js +36 -9
- package/dist/cli/commands/logsCommand.js +8 -28
- package/dist/cli/commands/mcpWizard.js +24 -2
- package/dist/cli/commands/modelCommand.js +1 -1
- package/dist/cli/commands/providerWizard.d.ts +1 -0
- package/dist/cli/commands/providerWizard.js +31 -5
- package/dist/cli/commands/runCommand.d.ts +1 -0
- package/dist/cli/commands/runCommand.js +32 -20
- package/dist/cli/commands/sessionPicker.d.ts +9 -0
- package/dist/cli/commands/sessionPicker.js +29 -0
- package/dist/cli/commands/settingsSummary.js +8 -4
- package/dist/cli/commands/skillConfirmRemove.js +4 -3
- package/dist/cli/commands/skillWizard.d.ts +2 -0
- package/dist/cli/commands/skillWizard.js +16 -6
- package/dist/cli/commands/streaming/idleTimer.d.ts +26 -0
- package/dist/cli/commands/streaming/idleTimer.js +24 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.d.ts +2 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.js +4 -2
- package/dist/cli/commands/streaming/turnOutcome.d.ts +10 -0
- package/dist/cli/commands/streaming/turnOutcome.js +13 -0
- package/dist/cli/commands/streaming/turnRuntime.js +6 -4
- package/dist/cli/commands/streaming.d.ts +13 -1
- package/dist/cli/commands/streaming.js +104 -44
- package/dist/cli/commands/wizardActions.d.ts +6 -0
- package/dist/cli/commands/wizardActions.js +6 -0
- package/dist/cli/commands/wizardPrompts.js +1 -1
- package/dist/cli/commands/wizardSuggestions.d.ts +8 -0
- package/dist/cli/commands/wizardSuggestions.js +62 -13
- package/dist/cli/commands/wizardTransition.d.ts +37 -0
- package/dist/cli/commands/wizardTransition.js +43 -0
- package/dist/cli/index.js +17 -3
- package/dist/cli/terminalTitle.d.ts +6 -0
- package/dist/cli/terminalTitle.js +36 -0
- package/dist/config/contextFiles.js +6 -3
- package/dist/config/endpointSecurity.d.ts +2 -0
- package/dist/config/endpointSecurity.js +28 -0
- package/dist/config/inputHistory.d.ts +3 -3
- package/dist/config/inputHistory.js +13 -17
- package/dist/config/modelDiscovery.d.ts +20 -0
- package/dist/config/modelDiscovery.js +51 -0
- package/dist/config/privateStorage.d.ts +6 -0
- package/dist/config/privateStorage.js +61 -0
- package/dist/config/providerPresets.d.ts +16 -4
- package/dist/config/providerPresets.js +263 -34
- package/dist/config/providers.d.ts +2 -0
- package/dist/config/providers.js +7 -0
- package/dist/config/settings.d.ts +22 -0
- package/dist/config/settings.js +33 -5
- package/dist/config/skillSettings.d.ts +6 -13
- package/dist/config/skillSettings.js +20 -19
- package/dist/config/updateCheck.js +4 -2
- package/dist/core/agent/budgets.d.ts +26 -0
- package/dist/core/agent/budgets.js +34 -0
- package/dist/core/agent/compaction.js +17 -3
- package/dist/core/agent/contextBudget.d.ts +8 -0
- package/dist/core/agent/contextBudget.js +29 -0
- package/dist/core/agent/errors.js +6 -2
- package/dist/core/agent/events.d.ts +12 -0
- package/dist/core/agent/toolOutputStore.d.ts +8 -0
- package/dist/core/agent/toolOutputStore.js +49 -11
- package/dist/core/agent/workState.d.ts +1 -1
- package/dist/core/agent/workState.js +0 -4
- package/dist/core/attachments/imageAttachments.d.ts +90 -0
- package/dist/core/attachments/imageAttachments.js +245 -0
- package/dist/core/attachments/readBlessings.d.ts +37 -0
- package/dist/core/attachments/readBlessings.js +79 -0
- package/dist/core/goal/completionPolicy.d.ts +0 -28
- package/dist/core/goal/completionPolicy.js +0 -88
- package/dist/core/goal/requestClassifier.d.ts +0 -3
- package/dist/core/goal/requestClassifier.js +0 -13
- package/dist/core/io/boundedRead.d.ts +15 -0
- package/dist/core/io/boundedRead.js +144 -0
- package/dist/core/limits/byteBudgets.d.ts +18 -0
- package/dist/core/limits/byteBudgets.js +18 -0
- package/dist/core/limits/textBudgets.d.ts +23 -0
- package/dist/core/limits/textBudgets.js +23 -0
- package/dist/core/log/llmLog.d.ts +10 -0
- package/dist/core/log/llmLog.js +59 -13
- package/dist/core/persistence/orderedFileWriter.d.ts +11 -0
- package/dist/core/persistence/orderedFileWriter.js +29 -0
- package/dist/core/process/backgroundRegistry.d.ts +35 -0
- package/dist/core/process/backgroundRegistry.js +194 -0
- package/dist/core/process/runBoundedProcess.d.ts +30 -0
- package/dist/core/process/runBoundedProcess.js +116 -0
- package/dist/core/safety/urlGuard.d.ts +2 -1
- package/dist/core/safety/urlGuard.js +3 -2
- package/dist/core/session/sessionSlimming.js +47 -4
- package/dist/core/session/sessionStore.d.ts +37 -0
- package/dist/core/session/sessionStore.js +136 -27
- package/dist/core/subagent/contracts.d.ts +116 -0
- package/dist/core/subagent/contracts.js +54 -0
- package/dist/core/subagent/executionProfiles.d.ts +28 -0
- package/dist/core/subagent/executionProfiles.js +45 -0
- package/dist/core/subagent/subagentCoordinator.d.ts +74 -0
- package/dist/core/subagent/subagentCoordinator.js +134 -0
- package/dist/core/subagent/subagentRunner.d.ts +211 -145
- package/dist/core/subagent/subagentRunner.js +228 -98
- package/dist/core/subagent/workspaceMutationPolicy.d.ts +11 -0
- package/dist/core/subagent/workspaceMutationPolicy.js +52 -0
- package/dist/core/tasks/taskStorage.js +12 -1
- package/dist/llm/client.d.ts +20 -29
- package/dist/llm/client.js +33 -29
- package/dist/llm/hazeTools.d.ts +2396 -67
- package/dist/llm/hazeTools.js +50 -37
- package/dist/llm/lsp.d.ts +23 -3
- package/dist/llm/lsp.js +73 -16
- package/dist/llm/lspTools.d.ts +96 -48
- package/dist/llm/mcp.d.ts +1 -1
- package/dist/llm/mcp.js +65 -16
- package/dist/llm/requestContext.d.ts +23 -1
- package/dist/llm/requestContext.js +50 -7
- package/dist/llm/systemPrompt.d.ts +4 -1
- package/dist/llm/systemPrompt.js +14 -4
- package/dist/llm/toolResultTypes.d.ts +1 -1
- package/dist/llm/tools/bashTool.d.ts +1210 -16
- package/dist/llm/tools/bashTool.js +43 -60
- package/dist/llm/tools/fetchTool.d.ts +26 -26
- package/dist/llm/tools/fetchTool.js +1 -1
- package/dist/llm/tools/fileToolShared.d.ts +2 -1
- package/dist/llm/tools/fileToolShared.js +5 -5
- package/dist/llm/tools/grepRunner.d.ts +20 -0
- package/dist/llm/tools/grepRunner.js +66 -0
- package/dist/llm/tools/outputCap.d.ts +2 -6
- package/dist/llm/tools/outputCap.js +2 -6
- package/dist/llm/tools/processTool.d.ts +1077 -0
- package/dist/llm/tools/processTool.js +41 -0
- package/dist/llm/tools/toolContext.d.ts +8 -0
- package/dist/llm/tools/toolContext.js +22 -11
- package/dist/llm/tools/workspaceFile.d.ts +9 -1
- package/dist/llm/tools/workspaceFile.js +33 -4
- package/dist/llm/webFetch.js +24 -11
- package/dist/llm/workerContext.d.ts +18 -0
- package/dist/llm/workerContext.js +55 -0
- package/dist/skills/SkillLoader.d.ts +2 -2
- package/dist/skills/SkillLoader.js +17 -5
- package/dist/skills/SkillRegistry.d.ts +3 -2
- package/dist/skills/SkillRegistry.js +53 -10
- package/dist/skills/builder/SkillBuilder.d.ts +3 -0
- package/dist/skills/builder/SkillBuilder.js +23 -5
- package/dist/skills/skillTools.js +11 -4
- package/dist/skills/types.d.ts +11 -1
- package/dist/ui/components/TextInput.d.ts +5 -2
- package/dist/ui/components/TextInput.js +76 -8
- package/dist/utils/format.d.ts +2 -0
- package/dist/utils/format.js +8 -0
- package/dist/utils/fs.d.ts +0 -1
- package/dist/utils/fs.js +0 -4
- package/dist/utils/openPath.d.ts +2 -0
- package/dist/utils/openPath.js +9 -0
- package/dist/utils/path.d.ts +2 -0
- package/dist/utils/path.js +9 -4
- package/package.json +1 -1
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function terminalTitleLabel(cwd: string): string;
|
|
2
|
+
/**
|
|
3
|
+
* Set the terminal title for the duration of this run. No-op when stdout is
|
|
4
|
+
* not a TTY (headless, piped, or redirected runs).
|
|
5
|
+
*/
|
|
6
|
+
export declare function installTerminalTitle(label: string): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
/**
|
|
3
|
+
* Terminal tab/window title for interactive runs. Uses the OSC 0 escape
|
|
4
|
+
* sequence, understood by xterm-compatible terminals (Terminal.app, iTerm2,
|
|
5
|
+
* VS Code, etc.). Guarded to real TTYs so piped/redirected output is never
|
|
6
|
+
* polluted, and cleared on exit so the tab falls back to the shell's own
|
|
7
|
+
* naming instead of showing a stale haze title.
|
|
8
|
+
*/
|
|
9
|
+
function titleEscape(label) {
|
|
10
|
+
return `\u001B]0;${label}\u0007`;
|
|
11
|
+
}
|
|
12
|
+
export function terminalTitleLabel(cwd) {
|
|
13
|
+
// eslint-disable-next-line no-control-regex -- stripping control characters is the point here.
|
|
14
|
+
const dir = basename(cwd).replace(/[\u0000-\u001F\u007F]/g, '').trim();
|
|
15
|
+
return `haze${dir ? ` - ${dir}` : ''}`;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Set the terminal title for the duration of this run. No-op when stdout is
|
|
19
|
+
* not a TTY (headless, piped, or redirected runs).
|
|
20
|
+
*/
|
|
21
|
+
export function installTerminalTitle(label) {
|
|
22
|
+
if (!process.stdout.isTTY)
|
|
23
|
+
return;
|
|
24
|
+
process.stdout.write(titleEscape(label));
|
|
25
|
+
process.once('exit', () => {
|
|
26
|
+
// Re-check at exit time: the terminal may no longer be attached.
|
|
27
|
+
if (!process.stdout.isTTY)
|
|
28
|
+
return;
|
|
29
|
+
try {
|
|
30
|
+
process.stdout.write(titleEscape(''));
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// Best effort only: title restoration must never break shutdown.
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -3,6 +3,7 @@ import os from 'node:os';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import crypto from 'node:crypto';
|
|
5
5
|
import { HAZE_DIR } from './paths.js';
|
|
6
|
+
import { readUtf8Prefix } from '../core/io/boundedRead.js';
|
|
6
7
|
const DEFAULT_BUDGET_THRESHOLD = 0.15;
|
|
7
8
|
function resolveBudgetThreshold(explicit) {
|
|
8
9
|
if (typeof explicit === 'number' && Number.isFinite(explicit))
|
|
@@ -54,11 +55,13 @@ async function readContextCandidates(candidates, seen = new Set(), options = {})
|
|
|
54
55
|
if (loadedSignature === signature)
|
|
55
56
|
continue;
|
|
56
57
|
options.onContextFileRead?.(displayedPath);
|
|
57
|
-
const
|
|
58
|
+
const prefix = await readUtf8Prefix(absolute, MAX_CONTEXT_FILE_CHARS * 4);
|
|
59
|
+
const content = prefix.content.slice(0, MAX_CONTEXT_FILE_CHARS);
|
|
60
|
+
const truncated = prefix.truncated || prefix.content.length > MAX_CONTEXT_FILE_CHARS;
|
|
58
61
|
const file = {
|
|
59
62
|
path: displayedPath,
|
|
60
|
-
content:
|
|
61
|
-
? `${content
|
|
63
|
+
content: truncated
|
|
64
|
+
? `${content}\n\n[Context file truncated at ${MAX_CONTEXT_FILE_CHARS} characters; source is ${prefix.totalBytes} bytes]`
|
|
62
65
|
: content,
|
|
63
66
|
signature,
|
|
64
67
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function hasCredentials(credentials) {
|
|
2
|
+
if (typeof credentials === 'string')
|
|
3
|
+
return credentials.trim().length > 0;
|
|
4
|
+
if (Array.isArray(credentials))
|
|
5
|
+
return credentials.some(item => typeof item === 'object' && item != null && 'value' in item && String(item.value).trim().length > 0);
|
|
6
|
+
return Boolean(credentials);
|
|
7
|
+
}
|
|
8
|
+
export function isLoopbackHostname(hostname) {
|
|
9
|
+
const host = hostname.replace(/^\[|\]$/g, '').toLowerCase();
|
|
10
|
+
if (host === 'localhost' || host.endsWith('.localhost') || host === '::1')
|
|
11
|
+
return true;
|
|
12
|
+
const parts = host.split('.').map(Number);
|
|
13
|
+
return parts.length === 4 && parts.every(part => Number.isInteger(part) && part >= 0 && part <= 255) && parts[0] === 127;
|
|
14
|
+
}
|
|
15
|
+
export function assertCredentialedEndpointSecure(urlValue, credentials) {
|
|
16
|
+
let url;
|
|
17
|
+
try {
|
|
18
|
+
url = new URL(urlValue);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new Error(`Invalid endpoint URL: ${urlValue}`);
|
|
22
|
+
}
|
|
23
|
+
if (!['http:', 'https:'].includes(url.protocol))
|
|
24
|
+
throw new Error(`Unsupported endpoint protocol: ${url.protocol}`);
|
|
25
|
+
if (url.protocol === 'http:' && hasCredentials(credentials) && !isLoopbackHostname(url.hostname)) {
|
|
26
|
+
throw new Error(`Refusing to send credentials over plaintext HTTP to remote endpoint ${url.origin}. Use HTTPS or a loopback endpoint.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const INPUT_HISTORY_FILE: string;
|
|
2
|
-
export declare function readInputHistory(): Promise<string[]>;
|
|
3
|
-
export declare function writeInputHistory(history: string[]): Promise<void>;
|
|
4
|
-
export declare function addInputHistoryItem(item: string): Promise<string[]>;
|
|
2
|
+
export declare function readInputHistory(file?: string): Promise<string[]>;
|
|
3
|
+
export declare function writeInputHistory(history: string[], file?: string): Promise<void>;
|
|
4
|
+
export declare function addInputHistoryItem(item: string, file?: string): Promise<string[]>;
|
|
@@ -1,37 +1,33 @@
|
|
|
1
1
|
import fs from 'fs-extra';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { HAZE_DIR } from './paths.js';
|
|
4
|
+
import { tightenPrivateFile, writePrivateJsonAtomic } from './privateStorage.js';
|
|
4
5
|
const HISTORY_DIR = path.join(HAZE_DIR, 'history');
|
|
5
6
|
export const INPUT_HISTORY_FILE = path.join(HISTORY_DIR, 'input-history.json');
|
|
6
7
|
const MAX_HISTORY_ITEMS = 500;
|
|
7
|
-
const DISABLE_PERSISTENT_HISTORY = process.env.VITEST === 'true' || process.env.NODE_ENV === 'test';
|
|
8
|
-
let testHistory = [];
|
|
9
8
|
function normalizeHistory(value) {
|
|
10
9
|
if (!Array.isArray(value))
|
|
11
10
|
return [];
|
|
12
11
|
return value.filter((item) => typeof item === 'string' && item.trim().length > 0);
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
// The storage path is injectable so tests isolate via temp files instead of
|
|
14
|
+
// production switches on test-env globals (CR-011).
|
|
15
|
+
export async function readInputHistory(file = INPUT_HISTORY_FILE) {
|
|
16
|
+
if (await fs.pathExists(file))
|
|
17
|
+
await tightenPrivateFile(file);
|
|
18
|
+
const data = await fs.readJson(file).catch(() => []);
|
|
18
19
|
return normalizeHistory(data).slice(-MAX_HISTORY_ITEMS);
|
|
19
20
|
}
|
|
20
|
-
export async function writeInputHistory(history) {
|
|
21
|
+
export async function writeInputHistory(history, file = INPUT_HISTORY_FILE) {
|
|
21
22
|
const normalized = normalizeHistory(history).slice(-MAX_HISTORY_ITEMS);
|
|
22
|
-
|
|
23
|
-
testHistory = normalized;
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
await fs.ensureDir(HISTORY_DIR);
|
|
27
|
-
await fs.writeJson(INPUT_HISTORY_FILE, normalized, { spaces: 2 });
|
|
23
|
+
await writePrivateJsonAtomic(file, normalized);
|
|
28
24
|
}
|
|
29
|
-
export async function addInputHistoryItem(item) {
|
|
25
|
+
export async function addInputHistoryItem(item, file = INPUT_HISTORY_FILE) {
|
|
30
26
|
const trimmed = item.trim();
|
|
31
27
|
if (!trimmed)
|
|
32
|
-
return readInputHistory();
|
|
33
|
-
const current = await readInputHistory();
|
|
28
|
+
return readInputHistory(file);
|
|
29
|
+
const current = await readInputHistory(file);
|
|
34
30
|
const next = current[current.length - 1] === trimmed ? current : [...current, trimmed];
|
|
35
|
-
await writeInputHistory(next);
|
|
31
|
+
await writeInputHistory(next, file);
|
|
36
32
|
return next.slice(-MAX_HISTORY_ITEMS);
|
|
37
33
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { HazeProviderSettings } from './settings.js';
|
|
2
|
+
/**
|
|
3
|
+
* Model discovery against the OpenAI-compatible `/models` endpoint every
|
|
4
|
+
* provider in haze exposes. Used to turn "add models" from a memorize-and-type
|
|
5
|
+
* step into a pick-from-list step; callers fall back to manual input when the
|
|
6
|
+
* endpoint is unavailable.
|
|
7
|
+
*/
|
|
8
|
+
export type ModelDiscoveryResult = {
|
|
9
|
+
status: 'ok';
|
|
10
|
+
models: string[];
|
|
11
|
+
} | {
|
|
12
|
+
status: 'failed';
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function modelsEndpointUrl(baseUrl: string): string;
|
|
16
|
+
export declare function parseModelsBody(body: unknown): string[];
|
|
17
|
+
export declare function discoverProviderModels(provider: Pick<HazeProviderSettings, 'url' | 'key'>, options?: {
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
fetchImpl?: typeof fetch;
|
|
20
|
+
}): Promise<ModelDiscoveryResult>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const DEFAULT_TIMEOUT_MS = 5000;
|
|
2
|
+
/** Discovery feeds a type-filtered picker; cap the stored list, not the UX. */
|
|
3
|
+
const MAX_DISCOVERED_MODELS = 500;
|
|
4
|
+
export function modelsEndpointUrl(baseUrl) {
|
|
5
|
+
return `${baseUrl.trim().replace(/\/+$/, '')}/models`;
|
|
6
|
+
}
|
|
7
|
+
export function parseModelsBody(body) {
|
|
8
|
+
const data = body?.data;
|
|
9
|
+
if (!Array.isArray(data))
|
|
10
|
+
return [];
|
|
11
|
+
const ids = data
|
|
12
|
+
.map(item => item?.id)
|
|
13
|
+
.filter((id) => typeof id === 'string' && id.trim().length > 0)
|
|
14
|
+
.map(id => id.trim());
|
|
15
|
+
return [...new Set(ids)].sort((a, b) => a.localeCompare(b)).slice(0, MAX_DISCOVERED_MODELS);
|
|
16
|
+
}
|
|
17
|
+
function describeFetchError(error) {
|
|
18
|
+
if (error instanceof Error) {
|
|
19
|
+
if (error.name === 'TimeoutError' || error.name === 'AbortError')
|
|
20
|
+
return 'timed out';
|
|
21
|
+
return error.message;
|
|
22
|
+
}
|
|
23
|
+
return String(error);
|
|
24
|
+
}
|
|
25
|
+
export async function discoverProviderModels(provider, options = {}) {
|
|
26
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
27
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
28
|
+
let response;
|
|
29
|
+
try {
|
|
30
|
+
response = await fetchImpl(modelsEndpointUrl(provider.url), {
|
|
31
|
+
headers: { accept: 'application/json', ...(provider.key ? { authorization: `Bearer ${provider.key}` } : {}) },
|
|
32
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return { status: 'failed', error: describeFetchError(error) };
|
|
37
|
+
}
|
|
38
|
+
if (!response.ok)
|
|
39
|
+
return { status: 'failed', error: `endpoint returned HTTP ${response.status}` };
|
|
40
|
+
let body;
|
|
41
|
+
try {
|
|
42
|
+
body = await response.json();
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return { status: 'failed', error: 'endpoint returned no JSON' };
|
|
46
|
+
}
|
|
47
|
+
const models = parseModelsBody(body);
|
|
48
|
+
if (models.length === 0)
|
|
49
|
+
return { status: 'failed', error: 'endpoint returned no models' };
|
|
50
|
+
return { status: 'ok', models };
|
|
51
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function ensurePrivateDir(dir: string): Promise<void>;
|
|
2
|
+
export declare function ensurePrivateFile(file: string): Promise<void>;
|
|
3
|
+
export declare function tightenPrivateFile(file: string): Promise<void>;
|
|
4
|
+
export declare function appendPrivateFile(file: string, content: string): Promise<void>;
|
|
5
|
+
export declare function writePrivateFileAtomic(file: string, content: string): Promise<void>;
|
|
6
|
+
export declare function writePrivateJsonAtomic(file: string, value: unknown): Promise<void>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { HAZE_DIR } from './paths.js';
|
|
4
|
+
const DIRECTORY_MODE = 0o700;
|
|
5
|
+
const FILE_MODE = 0o600;
|
|
6
|
+
export async function ensurePrivateDir(dir) {
|
|
7
|
+
const absolute = path.resolve(dir);
|
|
8
|
+
await fs.mkdir(absolute, { recursive: true, mode: DIRECTORY_MODE });
|
|
9
|
+
if (process.platform === 'win32')
|
|
10
|
+
return;
|
|
11
|
+
const hazeRoot = path.resolve(HAZE_DIR);
|
|
12
|
+
const relative = path.relative(hazeRoot, absolute);
|
|
13
|
+
if (!relative.startsWith('..') && !path.isAbsolute(relative)) {
|
|
14
|
+
await fs.chmod(hazeRoot, DIRECTORY_MODE);
|
|
15
|
+
let current = hazeRoot;
|
|
16
|
+
for (const segment of relative.split(path.sep).filter(Boolean)) {
|
|
17
|
+
current = path.join(current, segment);
|
|
18
|
+
await fs.chmod(current, DIRECTORY_MODE);
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
await fs.chmod(absolute, DIRECTORY_MODE);
|
|
23
|
+
}
|
|
24
|
+
export async function ensurePrivateFile(file) {
|
|
25
|
+
await ensurePrivateDir(path.dirname(file));
|
|
26
|
+
const handle = await fs.open(file, 'a', FILE_MODE);
|
|
27
|
+
await handle.close();
|
|
28
|
+
if (process.platform !== 'win32')
|
|
29
|
+
await fs.chmod(file, FILE_MODE);
|
|
30
|
+
}
|
|
31
|
+
export async function tightenPrivateFile(file) {
|
|
32
|
+
await ensurePrivateDir(path.dirname(file));
|
|
33
|
+
if (process.platform !== 'win32')
|
|
34
|
+
await fs.chmod(file, FILE_MODE);
|
|
35
|
+
}
|
|
36
|
+
export async function appendPrivateFile(file, content) {
|
|
37
|
+
await ensurePrivateFile(file);
|
|
38
|
+
await fs.appendFile(file, content, { encoding: 'utf8', mode: FILE_MODE });
|
|
39
|
+
}
|
|
40
|
+
export async function writePrivateFileAtomic(file, content) {
|
|
41
|
+
await ensurePrivateDir(path.dirname(file));
|
|
42
|
+
const temp = path.join(path.dirname(file), `.${path.basename(file)}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`);
|
|
43
|
+
let handle;
|
|
44
|
+
try {
|
|
45
|
+
handle = await fs.open(temp, 'wx', FILE_MODE);
|
|
46
|
+
await handle.writeFile(content, 'utf8');
|
|
47
|
+
await handle.sync();
|
|
48
|
+
await handle.close();
|
|
49
|
+
handle = undefined;
|
|
50
|
+
await fs.rename(temp, file);
|
|
51
|
+
if (process.platform !== 'win32')
|
|
52
|
+
await fs.chmod(file, FILE_MODE);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
await handle?.close().catch(() => undefined);
|
|
56
|
+
await fs.rm(temp, { force: true }).catch(() => undefined);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export async function writePrivateJsonAtomic(file, value) {
|
|
60
|
+
await writePrivateFileAtomic(file, `${JSON.stringify(value, null, 2)}\n`);
|
|
61
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Known provider presets derived from community conventions (nanocoder, OpenRouter docs, etc.)
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Known provider presets derived from community conventions (nanocoder, OpenRouter docs, etc.),
|
|
3
|
+
* enriched from Pi's built-in provider registry and generated model catalog
|
|
4
|
+
* (https://github.com/earendil-works/pi, `packages/ai` — provider ids, base URLs,
|
|
5
|
+
* API-key env var conventions, and current model ids).
|
|
6
|
+
* Each preset carries a pre-configured OpenAI-compatible base URL so users only need to supply
|
|
7
|
+
* an API key and model names. Local/keyless providers have sensible localhost defaults.
|
|
5
8
|
*/
|
|
6
9
|
export interface ProviderPreset {
|
|
7
10
|
/** Unique identifier used as the selection value. */
|
|
@@ -14,7 +17,16 @@ export interface ProviderPreset {
|
|
|
14
17
|
needsApiKey: boolean;
|
|
15
18
|
/** Hint shown when prompting for the API key. */
|
|
16
19
|
apiKeyHint?: string;
|
|
17
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Conventional environment variable holding this provider's API key (from Pi's
|
|
22
|
+
* auth registry). Informational only — haze never reads provider env vars itself.
|
|
23
|
+
*/
|
|
24
|
+
apiKeyEnvVar?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Curated model ids. Pinned atop the live /models discovery picker when the
|
|
27
|
+
* endpoint actually serves them (stale entries simply don't pin), and shown
|
|
28
|
+
* as type-in examples when discovery fails.
|
|
29
|
+
*/
|
|
18
30
|
suggestedModels?: string[];
|
|
19
31
|
/** Category for grouping in the picker. */
|
|
20
32
|
category: 'cloud' | 'local';
|