@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
@@ -0,0 +1,247 @@
1
+ /**
2
+ * GitHub Copilot provider implementation for Hem.
3
+ *
4
+ * Wraps the @github/copilot-sdk CopilotClient behind the {@link Provider}
5
+ * interface. Provides full feature parity with OpenCodeProvider:
6
+ * - MCP server registration (hem-broadcast) per session
7
+ * - Per-agent permission profiles (mapped from agent name to Copilot SDK permissions)
8
+ * - Broadcast tool interception via `onPreToolUse` hook → normalized SSE events
9
+ * - Terminable SSE event stream via central emitter pattern
10
+ * - Verbose logging throughout
11
+ *
12
+ * Aligns with Dispatch's ProviderInstance pattern:
13
+ * - `createSession()` — creates a Copilot session with full MCP + permission config
14
+ * - `prompt(sessionId, text, { agent })` — sets agent context, sends prompt, waits for idle
15
+ * - `cleanup()` — destroys all sessions, terminates SSE generators, stops the client
16
+ *
17
+ * Authentication options (checked in order by the SDK):
18
+ * 1. COPILOT_GITHUB_TOKEN / GH_TOKEN / GITHUB_TOKEN env vars
19
+ * 2. Logged-in Copilot CLI user (default — no token needed)
20
+ */
21
+ import type { Provider, ProviderConfig } from "./types.js";
22
+ /** Permission request kind from the Copilot SDK. */
23
+ type PermissionKind = "shell" | "write" | "mcp" | "read" | "url" | "custom-tool";
24
+ /** Minimal permission request shape. */
25
+ interface PermissionRequest {
26
+ kind: PermissionKind;
27
+ command?: string;
28
+ [key: string]: unknown;
29
+ }
30
+ /** Minimal permission result returned to the SDK. */
31
+ interface PermissionResult {
32
+ kind: "approved" | "denied-by-rules";
33
+ }
34
+ /**
35
+ * Minimal interface for a Copilot session, matching the subset of
36
+ * `CopilotSession` from `@github/copilot-sdk` used by the provider.
37
+ */
38
+ export interface CopilotSessionLike {
39
+ sessionId: string;
40
+ send(options: {
41
+ prompt: string;
42
+ }): Promise<string>;
43
+ abort(): Promise<void>;
44
+ destroy(): Promise<void>;
45
+ getMessages(): Promise<Array<{
46
+ type: string;
47
+ data?: {
48
+ content?: string;
49
+ };
50
+ }>>;
51
+ on(eventType: string, handler: (...args: unknown[]) => void): () => void;
52
+ }
53
+ /**
54
+ * Minimal interface for a Copilot client, matching the subset of
55
+ * `CopilotClient` from `@github/copilot-sdk` used by the provider.
56
+ */
57
+ export interface CopilotClientLike {
58
+ createSession(config?: {
59
+ model?: string;
60
+ workingDirectory?: string;
61
+ onPermissionRequest?: (request: PermissionRequest, ctx: {
62
+ sessionId: string;
63
+ }) => PermissionResult | Promise<PermissionResult>;
64
+ mcpServers?: Record<string, {
65
+ type?: string;
66
+ command?: string;
67
+ args?: string[];
68
+ tools?: string[];
69
+ }>;
70
+ hooks?: {
71
+ onPreToolUse?: (input: {
72
+ toolName: string;
73
+ toolArgs: unknown;
74
+ }, ctx: {
75
+ sessionId: string;
76
+ }) => Promise<{
77
+ permissionDecision?: string;
78
+ } | void> | void;
79
+ };
80
+ }): Promise<CopilotSessionLike>;
81
+ deleteSession(sessionId: string): Promise<void>;
82
+ on(eventType: string, handler: (...args: unknown[]) => void): () => void;
83
+ }
84
+ /**
85
+ * Factory function type for creating a CopilotClient instance.
86
+ * Accepts an optional GitHub token (used when an env-var token is found).
87
+ * Returns an object with a client and stop method.
88
+ * Overridable in tests.
89
+ */
90
+ export type CreateCopilotClientFn = (token?: string) => Promise<{
91
+ client: CopilotClientLike;
92
+ stop: () => Promise<void>;
93
+ }>;
94
+ /**
95
+ * Provider implementation backed by the GitHub Copilot SDK.
96
+ *
97
+ * Provides full feature parity with OpenCodeProvider:
98
+ * - MCP server: hem-broadcast registered on every session
99
+ * - Per-agent permissions: mapped from agent name via onPermissionRequest
100
+ * - Broadcast relay: onPreToolUse hook emits normalized message.part.updated events
101
+ * - SSE events: central emitter with terminable generators
102
+ * - Verbose logging throughout
103
+ *
104
+ * When no token is found in environment variables the SDK uses the
105
+ * Copilot CLI's own auth state (set up via `gh auth login`).
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * const provider = await CopilotProvider.create(config);
110
+ * const sessionId = await provider.createSession();
111
+ * const result = await provider.prompt(sessionId, "Explain this code", { agent: "hem-doc" });
112
+ * await provider.cleanup();
113
+ * ```
114
+ */
115
+ export declare class CopilotProvider implements Provider {
116
+ private _config;
117
+ private _client;
118
+ private _stopFn;
119
+ private _factory;
120
+ private _sessions;
121
+ private _sessionMeta;
122
+ private _modelValue;
123
+ private _modelDetected;
124
+ private _eventHandlers;
125
+ private _sseCleanupHandlers;
126
+ /** Low-level session operations (implementing Provider interface). */
127
+ readonly session: Provider["session"];
128
+ /** SSE event subscription (implementing Provider interface). */
129
+ readonly event: Provider["event"];
130
+ /**
131
+ * Creates a new Copilot provider.
132
+ *
133
+ * @param config - Provider configuration (model, destination, permissions).
134
+ * @param factory - Optional client factory override for testing.
135
+ */
136
+ constructor(config: ProviderConfig, factory?: CreateCopilotClientFn);
137
+ get name(): string;
138
+ get model(): string | undefined;
139
+ get config(): ProviderConfig;
140
+ /**
141
+ * Creates and initializes a CopilotProvider.
142
+ * Mirrors Dispatch's `boot()` pattern — callers receive a ready-to-use
143
+ * provider without calling `initialize()` separately.
144
+ */
145
+ static create(config: ProviderConfig, factory?: CreateCopilotClientFn): Promise<CopilotProvider>;
146
+ /** Fan out an SSE event to all active subscribers. */
147
+ private _emit;
148
+ /**
149
+ * Build a Copilot SDK permission handler that enforces the OpenCode-equivalent
150
+ * permission profile for the given agent context.
151
+ *
152
+ * Defaults to approve-all when `agentContext.name` is not yet set (i.e., before
153
+ * the first `prompt()` call). In practice, all tool calls arrive after the
154
+ * prompt, so the agent name is always set before any permission request.
155
+ */
156
+ private _buildPermissionHandler;
157
+ /**
158
+ * Build the full session config for a new Copilot session.
159
+ *
160
+ * Includes:
161
+ * - MCP server: hem-broadcast (mirrors OpenCodeProvider's MCP config)
162
+ * - onPermissionRequest: agent-aware permission handler
163
+ * - hooks.onPreToolUse: intercepts broadcast tool calls → emits normalized SSE
164
+ * - workingDirectory: scoped to destination path
165
+ */
166
+ private _buildSessionConfig;
167
+ /**
168
+ * Starts the Copilot client.
169
+ *
170
+ * Checks `COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, and `GITHUB_TOKEN` environment
171
+ * variables and passes the token to the SDK when found. When no token is
172
+ * present the SDK uses the Copilot CLI's own auth state.
173
+ *
174
+ * Registers the client-level `session.created` listener once (used by agents
175
+ * that monitor child session creation via the SSE event stream).
176
+ *
177
+ * Idempotent — subsequent calls are no-ops if already initialized.
178
+ *
179
+ * @throws {Error} If the Copilot client fails to start.
180
+ */
181
+ initialize(): Promise<void>;
182
+ /**
183
+ * Create a new Copilot session with MCP server and permission configuration.
184
+ *
185
+ * Each session is created with:
186
+ * - hem-broadcast MCP server (matches OpenCodeProvider's MCP config)
187
+ * - A permission handler that enforces agent-specific rules once the agent
188
+ * name is set via `prompt()` or `session.promptAsync()`
189
+ * - An `onPreToolUse` hook for broadcast interception
190
+ * - SSE event listeners for session.idle and session.error
191
+ *
192
+ * @returns The session ID.
193
+ * @throws {Error} If the provider is not initialized.
194
+ */
195
+ createSession(): Promise<string>;
196
+ /**
197
+ * Send a prompt to a Copilot session and wait for the response.
198
+ *
199
+ * Sets the agent context from `options.agent` so the permission handler
200
+ * enforces the correct per-agent policy for all subsequent tool calls.
201
+ *
202
+ * Flow:
203
+ * 1. Set agent context (for permission enforcement)
204
+ * 2. `session.send()` — fires the prompt
205
+ * 3. Wait for `session.idle` or `session.error` event
206
+ * 4. `session.getMessages()` — fetch the completed response
207
+ * 5. Return the last `assistant.message` content, or null
208
+ *
209
+ * @param sessionId - The session ID returned by `createSession()`.
210
+ * @param text - The prompt text.
211
+ * @param options - Optional: `agent` sets the permission profile for this session.
212
+ */
213
+ prompt(sessionId: string, text: string, options?: {
214
+ agent?: string;
215
+ }): Promise<string | null>;
216
+ /**
217
+ * Shuts down the Copilot client and releases all resources.
218
+ *
219
+ * 1. Terminates all active SSE generators (unblocks pending iterators).
220
+ * 2. Destroys all active sessions.
221
+ * 3. Stops the Copilot client.
222
+ *
223
+ * After cleanup, the provider instance must not be reused.
224
+ * No-op if the provider was never initialized or already shut down.
225
+ */
226
+ cleanup(): Promise<void>;
227
+ /**
228
+ * Alias for `cleanup()` — kept for backward compatibility with callers
229
+ * that use the old `shutdown()` name.
230
+ */
231
+ shutdown(): Promise<void>;
232
+ /**
233
+ * Discovers a GitHub token from well-known environment variables.
234
+ *
235
+ * Returns `undefined` when no token is set — the SDK will then use the
236
+ * Copilot CLI's own auth state instead.
237
+ *
238
+ * @internal
239
+ */
240
+ static _findToken(): string | undefined;
241
+ /**
242
+ * Returns the name of the env var that provided the token, for logging.
243
+ * @internal
244
+ */
245
+ static _findTokenSource(): string;
246
+ }
247
+ export {};