@pruddiman/hem 0.0.1-beta-5671db0

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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/arbiter-agent.d.ts +72 -0
  3. package/dist/agents/arbiter-agent.js +149 -0
  4. package/dist/agents/architecture-agent.d.ts +148 -0
  5. package/dist/agents/architecture-agent.js +459 -0
  6. package/dist/agents/base-agent.d.ts +44 -0
  7. package/dist/agents/base-agent.js +57 -0
  8. package/dist/agents/crossref-agent.d.ts +140 -0
  9. package/dist/agents/crossref-agent.js +560 -0
  10. package/dist/agents/crossref-arbiter-agent.d.ts +72 -0
  11. package/dist/agents/crossref-arbiter-agent.js +147 -0
  12. package/dist/agents/documentation-agent.d.ts +55 -0
  13. package/dist/agents/documentation-agent.js +159 -0
  14. package/dist/agents/exploration-agent.d.ts +58 -0
  15. package/dist/agents/exploration-agent.js +102 -0
  16. package/dist/agents/grouping-agent.d.ts +167 -0
  17. package/dist/agents/grouping-agent.js +557 -0
  18. package/dist/agents/index-agent.d.ts +86 -0
  19. package/dist/agents/index-agent.js +360 -0
  20. package/dist/agents/organization-agent.d.ts +144 -0
  21. package/dist/agents/organization-agent.js +607 -0
  22. package/dist/auth.d.ts +372 -0
  23. package/dist/auth.js +1072 -0
  24. package/dist/broadcast-mcp.d.ts +21 -0
  25. package/dist/broadcast-mcp.js +59 -0
  26. package/dist/changelog.d.ts +85 -0
  27. package/dist/changelog.js +223 -0
  28. package/dist/decision-queue.d.ts +173 -0
  29. package/dist/decision-queue.js +265 -0
  30. package/dist/diff-scope.d.ts +24 -0
  31. package/dist/diff-scope.js +28 -0
  32. package/dist/discovery.d.ts +54 -0
  33. package/dist/discovery.js +405 -0
  34. package/dist/grouping.d.ts +37 -0
  35. package/dist/grouping.js +343 -0
  36. package/dist/helpers/format.d.ts +5 -0
  37. package/dist/helpers/format.js +13 -0
  38. package/dist/helpers/index.d.ts +11 -0
  39. package/dist/helpers/index.js +11 -0
  40. package/dist/helpers/parsing.d.ts +52 -0
  41. package/dist/helpers/parsing.js +128 -0
  42. package/dist/helpers/paths.d.ts +41 -0
  43. package/dist/helpers/paths.js +67 -0
  44. package/dist/helpers/strings.d.ts +45 -0
  45. package/dist/helpers/strings.js +97 -0
  46. package/dist/index.d.ts +135 -0
  47. package/dist/index.js +1087 -0
  48. package/dist/merge-utils.d.ts +22 -0
  49. package/dist/merge-utils.js +34 -0
  50. package/dist/orchestrator.d.ts +194 -0
  51. package/dist/orchestrator.js +1169 -0
  52. package/dist/output.d.ts +106 -0
  53. package/dist/output.js +243 -0
  54. package/dist/progress.d.ts +228 -0
  55. package/dist/progress.js +644 -0
  56. package/dist/providers/copilot.d.ts +247 -0
  57. package/dist/providers/copilot.js +598 -0
  58. package/dist/providers/index.d.ts +15 -0
  59. package/dist/providers/index.js +12 -0
  60. package/dist/providers/opencode.d.ts +156 -0
  61. package/dist/providers/opencode.js +416 -0
  62. package/dist/providers/types.d.ts +156 -0
  63. package/dist/providers/types.js +16 -0
  64. package/dist/resources.d.ts +76 -0
  65. package/dist/resources.js +151 -0
  66. package/dist/search-index.d.ts +71 -0
  67. package/dist/search-index.js +187 -0
  68. package/dist/search-mcp.d.ts +25 -0
  69. package/dist/search-mcp.js +100 -0
  70. package/dist/server-utils.d.ts +56 -0
  71. package/dist/server-utils.js +135 -0
  72. package/dist/session.d.ts +227 -0
  73. package/dist/session.js +370 -0
  74. package/dist/types.d.ts +272 -0
  75. package/dist/types.js +5 -0
  76. package/dist/worktree.d.ts +82 -0
  77. package/dist/worktree.js +187 -0
  78. package/package.json +45 -0
package/dist/auth.d.ts ADDED
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Authentication detection, first-run prompt, free model fetch, and preferences I/O.
3
+ * Implements US3 (Smart Authentication with Zero-Friction Onboarding).
4
+ */
5
+ import React from "react";
6
+ import type { OpencodeClient } from "@opencode-ai/sdk";
7
+ import type { AuthState, FreeModelInfo, ModelSelection, ProjectConfig, UserPreferences } from "./types.js";
8
+ /** Directory where Hem stores user preferences. */
9
+ export declare const CONFIG_DIR: string;
10
+ /** Full path to the preferences file. */
11
+ export declare const PREFERENCES_PATH: string;
12
+ /**
13
+ * Load user preferences from `~/.config/hem/preferences.json`.
14
+ *
15
+ * - Returns `null` if the file does not exist.
16
+ * - Auto-creates the `~/.config/hem/` directory if missing.
17
+ * - Handles JSON parse errors gracefully by returning `null`.
18
+ *
19
+ * @param configDir — Override for the config directory (used in tests).
20
+ *
21
+ * Reference: FR-023, plan.md lines 197-205.
22
+ */
23
+ export declare function loadPreferences(configDir?: string): Promise<UserPreferences | null>;
24
+ /**
25
+ * Save user preferences to `~/.config/hem/preferences.json`.
26
+ *
27
+ * Writes formatted JSON (2-space indent) for human readability.
28
+ * Auto-creates the `~/.config/hem/` directory if missing.
29
+ *
30
+ * @param prefs — The preferences object to persist.
31
+ * @param configDir — Override for the config directory (used in tests).
32
+ *
33
+ * Reference: FR-023, plan.md lines 197-205.
34
+ */
35
+ export declare function savePreferences(prefs: UserPreferences, configDir?: string): Promise<void>;
36
+ /** Default directory for project-local Hem configuration. */
37
+ export declare const PROJECT_CONFIG_DIR: string;
38
+ /** Full path to the project config file. */
39
+ export declare const PROJECT_CONFIG_PATH: string;
40
+ /**
41
+ * Load project-local configuration from `{cwd}/.hem/config.json`.
42
+ *
43
+ * - Returns `null` if the file does not exist.
44
+ * - Auto-creates the `.hem/` directory if missing.
45
+ * - Handles JSON parse errors gracefully by returning `null`.
46
+ *
47
+ * @param configDir — Override for the config directory (used in tests).
48
+ */
49
+ export declare function loadProjectConfig(configDir?: string): Promise<ProjectConfig | null>;
50
+ /**
51
+ * Save project-local configuration to `{cwd}/.hem/config.json`.
52
+ *
53
+ * Writes formatted JSON (2-space indent) for human readability.
54
+ * Auto-creates the `.hem/` directory if missing.
55
+ *
56
+ * @param config — The project config object to persist.
57
+ * @param configDir — Override for the config directory (used in tests).
58
+ */
59
+ export declare function saveProjectConfig(config: ProjectConfig, configDir?: string): Promise<void>;
60
+ /** URL for the Opencode model catalog. */
61
+ export declare const ZEN_CATALOG_URL = "https://opencode.ai/zen/v1/models";
62
+ /** Error message when the Zen catalog is unreachable. */
63
+ export declare const ZEN_FETCH_ERROR: string;
64
+ /**
65
+ * Custom error class thrown when the Opencode model catalog cannot be
66
+ * reached (network error, timeout, non-200 status, invalid JSON).
67
+ *
68
+ * Used to distinguish catalog connectivity failures from other errors so
69
+ * callers (e.g., `handleGenerate`) can present a clear, actionable message.
70
+ *
71
+ * Reference: spec.md line 122, contracts/cli-interface.md lines 212-214.
72
+ */
73
+ export declare class ZenCatalogError extends Error {
74
+ constructor(cause?: unknown);
75
+ }
76
+ /**
77
+ * Classify a model's data retention policy from its privacy metadata.
78
+ *
79
+ * - If a "zero-retention" field exists (truthy) → `"zero-retention"`
80
+ * - If "training" is mentioned in any privacy-related string → `"may-train"`
81
+ * - Otherwise → `"unknown"`
82
+ *
83
+ * @internal
84
+ */
85
+ export declare function classifyRetention(model: Record<string, any>): {
86
+ retention: FreeModelInfo["dataRetention"];
87
+ note: string;
88
+ };
89
+ /**
90
+ * Fetch free models from the Opencode model catalog.
91
+ *
92
+ * 1. Fetches from `https://opencode.ai/zen/v1/models` using the global `fetch` API.
93
+ * 2. Filters models where `pricing.input === 0 && pricing.output === 0`.
94
+ * 3. Classifies each model's data retention from privacy metadata.
95
+ * 4. Maps to `FreeModelInfo[]` with `providerID` always `"opencode"`.
96
+ * 5. Returns sorted with zero-retention models first.
97
+ *
98
+ * @param fetchFn — Override for the fetch function (used in tests).
99
+ *
100
+ * Reference: FR-024, FR-025, plan.md lines 216-233.
101
+ */
102
+ export declare function fetchFreeModels(fetchFn?: typeof globalThis.fetch): Promise<FreeModelInfo[]>;
103
+ /**
104
+ * Warning message template when a stored model is no longer available.
105
+ *
106
+ * Placeholders: `{providerID}` and `{modelID}` are replaced at runtime.
107
+ *
108
+ * Reference: spec.md line 123, contracts/cli-interface.md lines 208-209.
109
+ */
110
+ export declare const INVALID_MODEL_WARNING: string;
111
+ /**
112
+ * Validate that a stored model selection still corresponds to an available
113
+ * provider/model combination.
114
+ *
115
+ * Calls `client.config.providers()` and checks whether:
116
+ * 1. A provider with the stored `providerID` exists.
117
+ * 2. If that provider exposes a `models` map, the stored `modelID` is
118
+ * present in it — unless the modelID is `"default"`, which is always
119
+ * valid (it defers to the provider's default model).
120
+ *
121
+ * @param client — OpenCode SDK client instance.
122
+ * @param prefs — User preferences containing the stored model selection.
123
+ * @returns `true` if the stored model is valid, `false` otherwise.
124
+ *
125
+ * Reference: spec.md line 123, contracts/cli-interface.md lines 208-209.
126
+ */
127
+ export declare function validateStoredModel(client: OpencodeClient, prefs: UserPreferences): Promise<boolean>;
128
+ /**
129
+ * List available models for a provider by querying the OpenCode provider metadata.
130
+ * Returns [] if the provider has no models map or the API call fails.
131
+ */
132
+ export declare function listProviderModels(client: OpencodeClient, providerID: string): Promise<{
133
+ id: string;
134
+ name: string;
135
+ providerID?: string;
136
+ }[]>;
137
+ /**
138
+ * Error message template when authentication has expired.
139
+ *
140
+ * Placeholder: `{providerName}` is replaced at runtime with the provider
141
+ * identifier (e.g., "anthropic").
142
+ *
143
+ * Reference: spec.md line 124, contracts/cli-interface.md lines 205-206.
144
+ */
145
+ export declare const AUTH_EXPIRED_ERROR: string;
146
+ /**
147
+ * Custom error class thrown when an auth expiry is detected during
148
+ * the generation pipeline. Carries the provider name for the error
149
+ * message so callers can format the `AUTH_EXPIRED_ERROR` template.
150
+ */
151
+ export declare class AuthExpiredError extends Error {
152
+ /** The provider whose credentials have expired. */
153
+ readonly providerName: string;
154
+ constructor(providerName: string, cause?: unknown);
155
+ }
156
+ /**
157
+ * Determine whether an error indicates expired or invalid authentication.
158
+ *
159
+ * Checks for HTTP 401 (Unauthorized) and 403 (Forbidden) status codes
160
+ * in common error shapes returned by LLM provider APIs:
161
+ * - `error.status` — fetch-style Response errors
162
+ * - `error.statusCode` — Node.js HTTP errors
163
+ * - `error.code` — string codes like `"unauthorized"` / `"forbidden"`
164
+ * - `error.message` — substring match for "401", "403", "unauthorized",
165
+ * "forbidden", "expired", "invalid.*key", "authentication"
166
+ *
167
+ * @param error — The error to inspect (any type).
168
+ * @returns `true` if the error looks like an auth expiry / invalid credentials.
169
+ *
170
+ * Reference: spec.md line 124, contracts/cli-interface.md lines 205-206.
171
+ */
172
+ export declare function isAuthExpired(error: unknown): boolean;
173
+ /**
174
+ * Error message template when an API key is invalid or rejected.
175
+ *
176
+ * Placeholder: `{providerID}` is replaced at runtime with the provider
177
+ * identifier (e.g., "anthropic").
178
+ *
179
+ * Reference: spec.md line 127, contracts/cli-interface.md.
180
+ */
181
+ export declare const INVALID_API_KEY_ERROR: string;
182
+ /**
183
+ * Custom error class thrown when an API key is rejected by the provider.
184
+ *
185
+ * Used to distinguish API-key-specific auth failures from generic
186
+ * auth expiry (e.g., OAuth token expired). Carries the `providerID`
187
+ * so callers (e.g., `handleGenerate`) can format a clear, actionable
188
+ * error message.
189
+ *
190
+ * Reference: spec.md line 127.
191
+ */
192
+ export declare class InvalidApiKeyError extends Error {
193
+ /** The provider that rejected the API key. */
194
+ readonly providerID: string;
195
+ constructor(providerID: string, cause?: unknown);
196
+ }
197
+ /**
198
+ * Inject an API key for a provider into the OpenCode SDK, bypassing OAuth
199
+ * and all interactive prompts.
200
+ *
201
+ * The `model` string must be in `provider/model-name` format (e.g.,
202
+ * `"anthropic/claude-sonnet-4"`). The provider ID is extracted as
203
+ * everything before the first `/`.
204
+ *
205
+ * @param client — OpenCode SDK client instance.
206
+ * @param model — Model identifier in `provider/model` format.
207
+ * @param apiKey — API key to inject for the provider.
208
+ *
209
+ * Reference: FR-022a, plan.md lines 146-148, spec.md scenario 7.
210
+ */
211
+ export declare function injectApiKey(client: OpencodeClient, model: string, apiKey: string): Promise<void>;
212
+ /**
213
+ * Detect the current authentication state by querying the OpenCode SDK client.
214
+ *
215
+ * - Calls `client.config.providers()` to get the list of connected providers.
216
+ * - Maps them to `ConnectedProvider[]`.
217
+ * - Determines `hasCredentials` (true if any provider is connected).
218
+ * - Loads preferences via `loadPreferences()`.
219
+ * - Sets `isFirstRun` to true if no preferences exist AND no credentials detected.
220
+ * - Returns a complete `AuthState` object.
221
+ *
222
+ * @param client — OpenCode SDK client instance.
223
+ * @param configDir — Override for the config directory (used in tests).
224
+ *
225
+ * Reference: FR-020, plan.md lines 148-151.
226
+ */
227
+ export declare function detectAuthState(client: OpencodeClient, configDir?: string): Promise<AuthState>;
228
+ /**
229
+ * Resolve human-readable model and provider labels by querying the OpenCode
230
+ * provider metadata.
231
+ *
232
+ * When `model` has `modelID === "default"`, looks up the actual default model
233
+ * from the `default` map returned by `client.provider.list()`.
234
+ * When an explicit model is set, uses the metadata to find the human-readable name.
235
+ * Falls back to capitalized-ID logic if the lookup fails or the API call errors.
236
+ *
237
+ * @param client — OpenCode SDK client instance.
238
+ * @param model — The current resolved model selection (may be undefined).
239
+ * @returns An object with `modelLabel` and `providerLabel` strings.
240
+ */
241
+ export declare function resolveActualModel(client: OpencodeClient, model?: ModelSelection): Promise<{
242
+ modelLabel: string;
243
+ providerLabel: string;
244
+ }>;
245
+ /**
246
+ * Render an Ink component and wait for a result via a callback.
247
+ *
248
+ * Wraps Ink's `render()` in a Promise that resolves when the component
249
+ * invokes the callback. The Ink instance is cleaned up automatically.
250
+ *
251
+ * @param createComponent — A function that receives a `resolve` callback
252
+ * and returns a React element to render.
253
+ * @returns The value passed to the callback by the component.
254
+ *
255
+ * @internal
256
+ */
257
+ export declare function renderAndWait<T>(createComponent: (resolve: (value: T) => void) => React.ReactElement): Promise<T>;
258
+ /**
259
+ * Handle the "api-key" sub-flow of the first-run prompt.
260
+ *
261
+ * Renders the `ApiKeyInput` component to collect a provider ID and API key,
262
+ * then injects the key into the OpenCode SDK via `client.auth.set()`.
263
+ *
264
+ * @param client — OpenCode SDK client instance.
265
+ * @returns A `ModelSelection` with the chosen provider and a placeholder model ID.
266
+ *
267
+ * @internal
268
+ */
269
+ export declare function handleApiKeyFlow(client: OpencodeClient): Promise<ModelSelection>;
270
+ /**
271
+ * Handle the OAuth sub-flow of the first-run prompt.
272
+ *
273
+ * 1. Calls `client.config.providers()` to get a list of available OAuth providers.
274
+ * 2. Renders a provider selection prompt using Ink (list provider names,
275
+ * let user pick one) via the `OAuthProviderSelect` component.
276
+ * 3. Initiates OAuth by calling `client.provider.auth({ path: { id } })`
277
+ * (the SDK's OAuth authorize endpoint for the selected provider).
278
+ * 4. Waits for the callback/completion.
279
+ * 5. Returns a `ModelSelection` with the provider's default model.
280
+ *
281
+ * @param client — OpenCode SDK client instance.
282
+ * @returns A `ModelSelection` with the provider's default model.
283
+ *
284
+ * Reference: FR-018, plan.md lines 176-178.
285
+ */
286
+ export declare function handleOAuthFlow(client: OpencodeClient): Promise<ModelSelection>;
287
+ /**
288
+ * Handle the "free models" sub-flow of the first-run prompt.
289
+ *
290
+ * Fetches free models from the Opencode catalog via `fetchFreeModels()`,
291
+ * renders the `FreeModelPicker` with the results, and returns the user's
292
+ * selection as a `ModelSelection`.
293
+ *
294
+ * @param client — OpenCode SDK client instance (unused; models are fetched
295
+ * from the public Zen catalog).
296
+ * @param models — Optional list of free models (used for testing to skip
297
+ * the network fetch).
298
+ * @returns A `ModelSelection` with the chosen free model.
299
+ *
300
+ * Reference: FR-024, FR-025, plan.md lines 180-195.
301
+ */
302
+ export declare function handleFreeModelSelection(_client: OpencodeClient, models?: FreeModelInfo[]): Promise<ModelSelection>;
303
+ /**
304
+ * Orchestrate the first-run authentication flow.
305
+ *
306
+ * Renders the `AuthPrompt` component, waits for the user's choice, and
307
+ * dispatches to the appropriate sub-flow:
308
+ * - "oauth" → `handleOAuthFlow(client)`
309
+ * - "api-key" → renders `ApiKeyInput` and calls `client.auth.set()`
310
+ * - "free" → `handleFreeModelSelection(client)`
311
+ * - "exit" → returns null
312
+ *
313
+ * After successful authentication, saves the chosen model to preferences
314
+ * via `savePreferences()`.
315
+ *
316
+ * @param client — OpenCode SDK client instance.
317
+ * @param configDir — Override for the config directory (used in tests).
318
+ * @returns The chosen `ModelSelection`, or `null` if the user chose to exit.
319
+ *
320
+ * Reference: FR-018, FR-020, plan.md lines 140-196.
321
+ */
322
+ export declare function handleFirstRun(client: OpencodeClient, configDir?: string): Promise<ModelSelection | null>;
323
+ /**
324
+ * Handle `hem auth login` — interactive auth, standalone (no generation).
325
+ *
326
+ * Runs the same interactive flow as `handleFirstRun` but standalone:
327
+ * the user picks a method (OAuth, API key, free, or exit), authenticates,
328
+ * and preferences are saved. No documentation generation follows.
329
+ *
330
+ * @param client — OpenCode SDK client instance.
331
+ * @param configDir — Override for the config directory (used in tests).
332
+ *
333
+ * Reference: FR-021, contracts/cli-interface.md lines 14-19.
334
+ */
335
+ export declare function handleAuthLogin(client: OpencodeClient, configDir?: string): Promise<void>;
336
+ /**
337
+ * Handle `hem auth list` — show connected providers with status.
338
+ *
339
+ * Calls `client.config.providers()`, formats and prints connected providers
340
+ * with status (checkmark + name + auth method) per `contracts/cli-interface.md`
341
+ * lines 132-143, also shows the default model from preferences.
342
+ *
343
+ * Output format:
344
+ * ```
345
+ * Connected providers:
346
+ * ✓ anthropic Claude Pro/Max (OAuth)
347
+ * ✓ opencode Zen API Key
348
+ *
349
+ * Default model: anthropic/claude-sonnet-4
350
+ * ```
351
+ *
352
+ * @param client — OpenCode SDK client instance.
353
+ * @param configDir — Override for the config directory (used in tests).
354
+ * @param writeFn — Override for the output function (used in tests).
355
+ *
356
+ * Reference: FR-021, contracts/cli-interface.md lines 132-143.
357
+ */
358
+ export declare function handleAuthList(client: OpencodeClient, configDir?: string, writeFn?: (msg: string) => void): Promise<void>;
359
+ /**
360
+ * Handle `hem auth logout` — remove credentials.
361
+ *
362
+ * If `target` is provided, removes credentials for that specific provider.
363
+ * If no `target`, removes all credentials by iterating over connected providers.
364
+ * Prints a confirmation message.
365
+ *
366
+ * @param client — OpenCode SDK client instance.
367
+ * @param target — Optional provider ID to remove. If omitted, removes all.
368
+ * @param writeFn — Override for the output function (used in tests).
369
+ *
370
+ * Reference: FR-021, contracts/cli-interface.md lines 14-19, 141-143.
371
+ */
372
+ export declare function handleAuthLogout(client: OpencodeClient, target?: string, writeFn?: (msg: string) => void): Promise<void>;