axexec 1.0.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.
Files changed (102) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +196 -0
  3. package/bin/axexec +17 -0
  4. package/dist/agents/claude-code/adapter.d.ts +6 -0
  5. package/dist/agents/claude-code/adapter.js +71 -0
  6. package/dist/agents/claude-code/parse-event.d.ts +16 -0
  7. package/dist/agents/claude-code/parse-event.js +185 -0
  8. package/dist/agents/claude-code/types.d.ts +180 -0
  9. package/dist/agents/claude-code/types.js +112 -0
  10. package/dist/agents/codex/adapter.d.ts +6 -0
  11. package/dist/agents/codex/adapter.js +62 -0
  12. package/dist/agents/codex/item-parsers.d.ts +17 -0
  13. package/dist/agents/codex/item-parsers.js +126 -0
  14. package/dist/agents/codex/parse-event.d.ts +38 -0
  15. package/dist/agents/codex/parse-event.js +141 -0
  16. package/dist/agents/codex/types.d.ts +407 -0
  17. package/dist/agents/codex/types.js +188 -0
  18. package/dist/agents/copilot/adapter.d.ts +11 -0
  19. package/dist/agents/copilot/adapter.js +81 -0
  20. package/dist/agents/copilot/parse-event.d.ts +20 -0
  21. package/dist/agents/copilot/parse-event.js +137 -0
  22. package/dist/agents/copilot/stream-session.d.ts +10 -0
  23. package/dist/agents/copilot/stream-session.js +120 -0
  24. package/dist/agents/copilot/tail-file.d.ts +11 -0
  25. package/dist/agents/copilot/tail-file.js +52 -0
  26. package/dist/agents/copilot/transform-event.d.ts +19 -0
  27. package/dist/agents/copilot/transform-event.js +100 -0
  28. package/dist/agents/copilot/types.d.ts +320 -0
  29. package/dist/agents/copilot/types.js +220 -0
  30. package/dist/agents/copilot/watch-session.d.ts +26 -0
  31. package/dist/agents/copilot/watch-session.js +186 -0
  32. package/dist/agents/gemini/adapter.d.ts +6 -0
  33. package/dist/agents/gemini/adapter.js +78 -0
  34. package/dist/agents/gemini/parse-event.d.ts +18 -0
  35. package/dist/agents/gemini/parse-event.js +144 -0
  36. package/dist/agents/gemini/types.d.ts +162 -0
  37. package/dist/agents/gemini/types.js +103 -0
  38. package/dist/agents/opencode/adapter.d.ts +16 -0
  39. package/dist/agents/opencode/adapter.js +142 -0
  40. package/dist/agents/opencode/cleanup-session.d.ts +17 -0
  41. package/dist/agents/opencode/cleanup-session.js +41 -0
  42. package/dist/agents/opencode/create-session-start-event.d.ts +18 -0
  43. package/dist/agents/opencode/create-session-start-event.js +24 -0
  44. package/dist/agents/opencode/detect-empty-session.d.ts +25 -0
  45. package/dist/agents/opencode/detect-empty-session.js +42 -0
  46. package/dist/agents/opencode/map-error-to-event.d.ts +10 -0
  47. package/dist/agents/opencode/map-error-to-event.js +55 -0
  48. package/dist/agents/opencode/parse-message-part.d.ts +24 -0
  49. package/dist/agents/opencode/parse-message-part.js +112 -0
  50. package/dist/agents/opencode/parse-sse-event.d.ts +23 -0
  51. package/dist/agents/opencode/parse-sse-event.js +125 -0
  52. package/dist/agents/opencode/process-sse-events.d.ts +24 -0
  53. package/dist/agents/opencode/process-sse-events.js +66 -0
  54. package/dist/agents/opencode/server-types.d.ts +509 -0
  55. package/dist/agents/opencode/server-types.js +293 -0
  56. package/dist/agents/opencode/session-api.d.ts +56 -0
  57. package/dist/agents/opencode/session-api.js +151 -0
  58. package/dist/agents/opencode/spawn-server.d.ts +29 -0
  59. package/dist/agents/opencode/spawn-server.js +95 -0
  60. package/dist/agents/opencode/sse-client.d.ts +33 -0
  61. package/dist/agents/opencode/sse-client.js +145 -0
  62. package/dist/agents/registry.d.ts +15 -0
  63. package/dist/agents/registry.js +24 -0
  64. package/dist/cli.d.ts +15 -0
  65. package/dist/cli.js +119 -0
  66. package/dist/credentials/get-credential-environment.d.ts +13 -0
  67. package/dist/credentials/get-credential-environment.js +46 -0
  68. package/dist/credentials/install-credentials.d.ts +27 -0
  69. package/dist/credentials/install-credentials.js +102 -0
  70. package/dist/credentials/save-json-file.d.ts +11 -0
  71. package/dist/credentials/save-json-file.js +21 -0
  72. package/dist/credentials/types.d.ts +24 -0
  73. package/dist/credentials/types.js +4 -0
  74. package/dist/credentials/write-agent-credentials.d.ts +36 -0
  75. package/dist/credentials/write-agent-credentials.js +91 -0
  76. package/dist/determine-session-success.d.ts +15 -0
  77. package/dist/determine-session-success.js +25 -0
  78. package/dist/format-event-tsv.d.ts +23 -0
  79. package/dist/format-event-tsv.js +136 -0
  80. package/dist/parse-credentials.d.ts +13 -0
  81. package/dist/parse-credentials.js +63 -0
  82. package/dist/parse-iso-timestamp.d.ts +21 -0
  83. package/dist/parse-iso-timestamp.js +37 -0
  84. package/dist/resolve-binary.d.ts +29 -0
  85. package/dist/resolve-binary.js +46 -0
  86. package/dist/resolve-output-mode.d.ts +39 -0
  87. package/dist/resolve-output-mode.js +39 -0
  88. package/dist/run-agent.d.ts +32 -0
  89. package/dist/run-agent.js +146 -0
  90. package/dist/stream-agent.d.ts +20 -0
  91. package/dist/stream-agent.js +207 -0
  92. package/dist/types/adapter.d.ts +101 -0
  93. package/dist/types/adapter.js +14 -0
  94. package/dist/types/events.d.ts +82 -0
  95. package/dist/types/events.js +13 -0
  96. package/dist/types/options.d.ts +41 -0
  97. package/dist/types/options.js +4 -0
  98. package/dist/validate-agent.d.ts +20 -0
  99. package/dist/validate-agent.js +50 -0
  100. package/dist/write-event.d.ts +23 -0
  101. package/dist/write-event.js +43 -0
  102. package/package.json +79 -0
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Session file watcher for Copilot CLI.
3
+ *
4
+ * Watches ~/.copilot/session-state/ for new session files.
5
+ *
6
+ * Copilot CLI has two session file formats:
7
+ * - Old: ~/.copilot/session-state/<uuid>.jsonl
8
+ * - New (0.0.380+): ~/.copilot/session-state/<uuid>/events.jsonl
9
+ *
10
+ * This module supports both formats for compatibility.
11
+ */
12
+ import { existsSync, readdirSync, statSync, watch, } from "node:fs";
13
+ import { homedir } from "node:os";
14
+ import path from "node:path";
15
+ /** Copilot session state directory */
16
+ const SESSION_STATE_DIR = path.join(homedir(), ".copilot", "session-state");
17
+ /** Session events filename inside session directories (new format) */
18
+ const SESSION_EVENTS_FILE = "events.jsonl";
19
+ /**
20
+ * Get all existing session entries (both old .jsonl files and new directories).
21
+ * Returns entry names (not full paths) for comparison.
22
+ */
23
+ export function getExistingSessionFiles() {
24
+ try {
25
+ const entries = readdirSync(SESSION_STATE_DIR);
26
+ // Include both .jsonl files (old format) and directories (new format)
27
+ return new Set(entries.filter((entry) => {
28
+ if (entry.endsWith(".jsonl")) {
29
+ return true;
30
+ }
31
+ // Check if it's a directory (new format)
32
+ try {
33
+ const entryPath = path.join(SESSION_STATE_DIR, entry);
34
+ return statSync(entryPath).isDirectory();
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ }));
40
+ }
41
+ catch {
42
+ // Directory might not exist yet
43
+ return new Set();
44
+ }
45
+ }
46
+ /**
47
+ * Find a new session file that wasn't in the existing set.
48
+ * Supports both old format (.jsonl files) and new format (directories with events.jsonl).
49
+ */
50
+ function findNewSessionFile(existing) {
51
+ try {
52
+ const entries = readdirSync(SESSION_STATE_DIR);
53
+ for (const entry of entries) {
54
+ if (existing.has(entry)) {
55
+ continue;
56
+ }
57
+ const entryPath = path.join(SESSION_STATE_DIR, entry);
58
+ // Old format: direct .jsonl file
59
+ if (entry.endsWith(".jsonl")) {
60
+ return entryPath;
61
+ }
62
+ // New format: directory with events.jsonl inside
63
+ try {
64
+ if (statSync(entryPath).isDirectory()) {
65
+ const eventsFile = path.join(entryPath, SESSION_EVENTS_FILE);
66
+ if (existsSync(eventsFile)) {
67
+ return eventsFile;
68
+ }
69
+ }
70
+ }
71
+ catch {
72
+ // Entry might have been deleted, continue
73
+ }
74
+ }
75
+ }
76
+ catch {
77
+ // Directory might not exist
78
+ }
79
+ return undefined;
80
+ }
81
+ /**
82
+ * Wait for a new session file to appear.
83
+ *
84
+ * Uses both file watching and polling to detect new session files.
85
+ * Returns a promise that resolves with the new session file path.
86
+ */
87
+ export function waitForSessionFile(existingFiles, timeoutMs = 10_000) {
88
+ let watcher;
89
+ let pollInterval;
90
+ let timeoutId;
91
+ let eventsCheckInterval;
92
+ let resolved = false;
93
+ const cleanup = () => {
94
+ if (watcher) {
95
+ watcher.close();
96
+ watcher = undefined;
97
+ }
98
+ if (pollInterval) {
99
+ clearInterval(pollInterval);
100
+ pollInterval = undefined;
101
+ }
102
+ if (timeoutId) {
103
+ clearTimeout(timeoutId);
104
+ timeoutId = undefined;
105
+ }
106
+ if (eventsCheckInterval) {
107
+ clearInterval(eventsCheckInterval);
108
+ eventsCheckInterval = undefined;
109
+ }
110
+ };
111
+ const promise = new Promise((resolve, reject) => {
112
+ // Check immediately
113
+ const existing = findNewSessionFile(existingFiles);
114
+ if (existing) {
115
+ resolved = true;
116
+ resolve(existing);
117
+ return;
118
+ }
119
+ // Set up watcher (gracefully degrade to polling if directory doesn't exist)
120
+ try {
121
+ watcher = watch(SESSION_STATE_DIR, (eventType, filename) => {
122
+ if (resolved)
123
+ return;
124
+ if (eventType === "rename" &&
125
+ filename &&
126
+ !existingFiles.has(filename)) {
127
+ // Old format: direct .jsonl file
128
+ if (filename.endsWith(".jsonl")) {
129
+ resolved = true;
130
+ cleanup();
131
+ resolve(path.join(SESSION_STATE_DIR, filename));
132
+ return;
133
+ }
134
+ // New format: directory - check for events.jsonl inside
135
+ // Use polling to detect when events.jsonl appears (directory may be created first)
136
+ const eventsFile = path.join(SESSION_STATE_DIR, filename, SESSION_EVENTS_FILE);
137
+ const checkForEventsFile = () => {
138
+ if (resolved)
139
+ return;
140
+ if (existsSync(eventsFile)) {
141
+ resolved = true;
142
+ cleanup();
143
+ resolve(eventsFile);
144
+ }
145
+ };
146
+ // Check immediately and set up short polling for the events file
147
+ checkForEventsFile();
148
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- resolved may be set by checkForEventsFile
149
+ if (!resolved) {
150
+ eventsCheckInterval = setInterval(checkForEventsFile, 50);
151
+ }
152
+ }
153
+ });
154
+ watcher.on("error", () => {
155
+ // Watcher failed, continue with polling only
156
+ if (watcher) {
157
+ watcher.close();
158
+ watcher = undefined;
159
+ }
160
+ });
161
+ }
162
+ catch {
163
+ // Directory doesn't exist yet (ENOENT), continue with polling only
164
+ // Polling will detect the file once Copilot creates the directory
165
+ }
166
+ // Also poll periodically in case we miss the event
167
+ pollInterval = setInterval(() => {
168
+ if (resolved)
169
+ return;
170
+ const newFile = findNewSessionFile(existingFiles);
171
+ if (newFile) {
172
+ resolved = true;
173
+ cleanup();
174
+ resolve(newFile);
175
+ }
176
+ }, 100);
177
+ // Timeout
178
+ timeoutId = setTimeout(() => {
179
+ if (!resolved) {
180
+ cleanup();
181
+ reject(new Error("Timeout waiting for session file"));
182
+ }
183
+ }, timeoutMs);
184
+ });
185
+ return { promise, cleanup };
186
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Gemini adapter implementation.
3
+ *
4
+ * Implements the {@link AgentAdapter} interface for Gemini CLI.
5
+ */
6
+ export {};
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Gemini adapter implementation.
3
+ *
4
+ * Implements the {@link AgentAdapter} interface for Gemini CLI.
5
+ */
6
+ import { determineSessionSuccess } from "../../determine-session-success.js";
7
+ import { resolveBinary } from "../../resolve-binary.js";
8
+ import { registerAdapter } from "../registry.js";
9
+ import { createGeminiParser } from "./parse-event.js";
10
+ const GEMINI_INFO = {
11
+ id: "gemini",
12
+ name: "Gemini",
13
+ package: "@google/gemini-cli",
14
+ };
15
+ /**
16
+ * Environment variables to exclude by default.
17
+ *
18
+ * Gemini CLI has strict environment variable sanitization that blocks
19
+ * GH_TOKEN from reaching shell commands when GITHUB_SHA is set.
20
+ */
21
+ const GITHUB_SHA_EXCLUSION = ["GITHUB_SHA"];
22
+ /**
23
+ * Prepares the command to spawn Gemini CLI.
24
+ */
25
+ function prepareCommand(options) {
26
+ const cliArguments = [
27
+ "--output-format",
28
+ "stream-json",
29
+ // Required for non-interactive mode - stdin is not connected for approval prompts
30
+ "--yolo",
31
+ ];
32
+ // Add model flag if specified
33
+ // Gemini accepts: gemini-2.5-pro, gemini-2.5-flash, auto, pro, flash aliases
34
+ if (options.model) {
35
+ cliArguments.push("--model", options.model);
36
+ }
37
+ // Prompt must be last argument
38
+ cliArguments.push(options.prompt);
39
+ const environment = {};
40
+ // Pass through authentication environment variables.
41
+ // If none are set, Gemini CLI will handle auth errors on its own.
42
+ if (process.env["GEMINI_API_KEY"]) {
43
+ environment["GEMINI_API_KEY"] = process.env["GEMINI_API_KEY"];
44
+ }
45
+ if (process.env["GOOGLE_API_KEY"]) {
46
+ environment["GOOGLE_API_KEY"] = process.env["GOOGLE_API_KEY"];
47
+ }
48
+ if (process.env["GOOGLE_APPLICATION_CREDENTIALS"]) {
49
+ environment["GOOGLE_APPLICATION_CREDENTIALS"] =
50
+ process.env["GOOGLE_APPLICATION_CREDENTIALS"];
51
+ }
52
+ if (process.env["GOOGLE_CLOUD_PROJECT"]) {
53
+ environment["GOOGLE_CLOUD_PROJECT"] = process.env["GOOGLE_CLOUD_PROJECT"];
54
+ }
55
+ const bin = resolveBinary({
56
+ name: "gemini",
57
+ command: "gemini",
58
+ environmentVariable: "AXEXEC_GEMINI_PATH",
59
+ installHint: "npm install -g @google/gemini-cli",
60
+ });
61
+ return {
62
+ bin,
63
+ args: cliArguments,
64
+ env: environment,
65
+ // By default, exclude GITHUB_SHA to work around Gemini CLI's strict env sanitization.
66
+ // Use --preserve-github-sha to keep it.
67
+ envExclusions: options.preserveGithubSha ? undefined : GITHUB_SHA_EXCLUSION,
68
+ };
69
+ }
70
+ /** Gemini adapter */
71
+ const geminiAdapter = {
72
+ info: () => GEMINI_INFO,
73
+ prepareCommand,
74
+ createParser: createGeminiParser,
75
+ isSuccess: determineSessionSuccess,
76
+ };
77
+ // Self-register on import
78
+ registerAdapter(geminiAdapter);
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Event parser for Gemini CLI.
3
+ *
4
+ * Transforms raw Gemini CLI JSONL events into normalized {@link AxexecEvent} types.
5
+ * Uses stateful correlation to resolve tool names in tool.result events.
6
+ */
7
+ import type { EventParser } from "../../types/adapter.js";
8
+ /**
9
+ * Creates a parser for Gemini CLI events.
10
+ *
11
+ * Gemini CLI's tool_result events only include tool_id, not tool_name.
12
+ * This parser maintains a mapping from callId to toolName to resolve
13
+ * the tool name when parsing tool.result events.
14
+ * Uses a stateful timestamp parser to preserve event ordering when
15
+ * timestamps are missing.
16
+ */
17
+ declare function createGeminiParser(): EventParser;
18
+ export { createGeminiParser };
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Event parser for Gemini CLI.
3
+ *
4
+ * Transforms raw Gemini CLI JSONL events into normalized {@link AxexecEvent} types.
5
+ * Uses stateful correlation to resolve tool names in tool.result events.
6
+ */
7
+ import { createTimestampParser } from "../../parse-iso-timestamp.js";
8
+ import { GeminiEvent, isErrorEvent, isInitEvent, isMessageEvent, isResultEvent, isToolResultEvent, isToolUseEvent, } from "./types.js";
9
+ /**
10
+ * Creates a parser for Gemini CLI events.
11
+ *
12
+ * Gemini CLI's tool_result events only include tool_id, not tool_name.
13
+ * This parser maintains a mapping from callId to toolName to resolve
14
+ * the tool name when parsing tool.result events.
15
+ * Uses a stateful timestamp parser to preserve event ordering when
16
+ * timestamps are missing.
17
+ */
18
+ function createGeminiParser() {
19
+ // Map from callId (tool_id) to tool name for correlation
20
+ const toolNames = new Map();
21
+ // Stateful timestamp parser - falls back to previous timestamp when missing
22
+ const parseTimestamp = createTimestampParser();
23
+ return (line) => {
24
+ const trimmed = line.trim();
25
+ if (trimmed === "") {
26
+ return undefined;
27
+ }
28
+ let json;
29
+ try {
30
+ json = JSON.parse(trimmed);
31
+ }
32
+ catch {
33
+ return undefined;
34
+ }
35
+ const result = GeminiEvent.safeParse(json);
36
+ if (!result.success) {
37
+ return undefined;
38
+ }
39
+ const raw = result.data;
40
+ if (isInitEvent(raw)) {
41
+ return parseInitEvent(raw, parseTimestamp);
42
+ }
43
+ if (isMessageEvent(raw)) {
44
+ return parseMessageEvent(raw, parseTimestamp);
45
+ }
46
+ if (isToolUseEvent(raw)) {
47
+ return parseToolUseEvent(raw, toolNames, parseTimestamp);
48
+ }
49
+ if (isToolResultEvent(raw)) {
50
+ return parseToolResultEvent(raw, toolNames, parseTimestamp);
51
+ }
52
+ if (isErrorEvent(raw)) {
53
+ return parseErrorEvent(raw, parseTimestamp);
54
+ }
55
+ if (isResultEvent(raw)) {
56
+ return parseResultEvent(raw, parseTimestamp);
57
+ }
58
+ return undefined;
59
+ };
60
+ }
61
+ /** Parses an init event (task start) */
62
+ function parseInitEvent(event, parseTimestamp) {
63
+ return {
64
+ type: "session.start",
65
+ sessionId: event.session_id,
66
+ agent: "gemini",
67
+ model: event.model ?? "unknown",
68
+ provider: "google",
69
+ timestamp: parseTimestamp(event.timestamp),
70
+ };
71
+ }
72
+ /** Parses a message event (progress or skip) */
73
+ function parseMessageEvent(event, parseTimestamp) {
74
+ if (event.role === "user") {
75
+ return undefined;
76
+ }
77
+ return {
78
+ type: "agent.message",
79
+ content: event.content,
80
+ timestamp: parseTimestamp(event.timestamp),
81
+ };
82
+ }
83
+ /** Parses a tool use event and stores the callId -> toolName mapping */
84
+ function parseToolUseEvent(event, toolNames, parseTimestamp) {
85
+ // Store mapping for later correlation with tool.result
86
+ toolNames.set(event.tool_id, event.tool_name);
87
+ return {
88
+ type: "tool.call",
89
+ callId: event.tool_id,
90
+ tool: event.tool_name,
91
+ input: event.parameters,
92
+ timestamp: parseTimestamp(event.timestamp),
93
+ };
94
+ }
95
+ /** Parses a tool result event, resolving the tool name from stored mapping */
96
+ function parseToolResultEvent(event, toolNames, parseTimestamp) {
97
+ // Look up tool name from previous tool.call event
98
+ const toolName = toolNames.get(event.tool_id) ?? "unknown";
99
+ // Clean up the mapping to prevent memory leaks in long-running sessions
100
+ toolNames.delete(event.tool_id);
101
+ return {
102
+ type: "tool.result",
103
+ callId: event.tool_id,
104
+ tool: toolName,
105
+ output: event.output ?? event.error?.message,
106
+ success: event.status === "success",
107
+ timestamp: parseTimestamp(event.timestamp),
108
+ };
109
+ }
110
+ /** Parses an error event (task error or skip) */
111
+ function parseErrorEvent(event, parseTimestamp) {
112
+ if (event.severity === "warning") {
113
+ return undefined;
114
+ }
115
+ return {
116
+ type: "session.error",
117
+ code: "AGENT_ERROR",
118
+ message: event.message,
119
+ timestamp: parseTimestamp(event.timestamp),
120
+ };
121
+ }
122
+ /** Parses a result event (task completion or error) */
123
+ function parseResultEvent(event, parseTimestamp) {
124
+ const timestamp = parseTimestamp(event.timestamp);
125
+ if (event.status === "success") {
126
+ return {
127
+ type: "session.complete",
128
+ stats: {
129
+ durationMs: event.stats?.duration_ms ?? 0,
130
+ inputTokens: event.stats?.input_tokens,
131
+ outputTokens: event.stats?.output_tokens,
132
+ },
133
+ timestamp,
134
+ };
135
+ }
136
+ // Error or unknown status - treat as error
137
+ return {
138
+ type: "session.error",
139
+ code: "AGENT_ERROR",
140
+ message: event.error?.message ?? "Unknown error",
141
+ timestamp,
142
+ };
143
+ }
144
+ export { createGeminiParser };
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Raw event types from Gemini CLI's JSONL stream.
3
+ *
4
+ * These types represent the raw output from:
5
+ * `gemini --output-format stream-json "..."`
6
+ *
7
+ * The parser transforms these into normalized {@link AxexecEvent} types.
8
+ */
9
+ import { z } from "zod";
10
+ /** Init event - emitted at startup with session info */
11
+ declare const GeminiInitEvent: z.ZodObject<{
12
+ type: z.ZodLiteral<"init">;
13
+ timestamp: z.ZodString;
14
+ session_id: z.ZodString;
15
+ model: z.ZodOptional<z.ZodString>;
16
+ }, z.core.$strip>;
17
+ type GeminiInitEvent = z.infer<typeof GeminiInitEvent>;
18
+ /** Message event - user or assistant messages */
19
+ declare const GeminiMessageEvent: z.ZodObject<{
20
+ type: z.ZodLiteral<"message">;
21
+ role: z.ZodEnum<{
22
+ assistant: "assistant";
23
+ user: "user";
24
+ }>;
25
+ content: z.ZodString;
26
+ delta: z.ZodOptional<z.ZodBoolean>;
27
+ timestamp: z.ZodString;
28
+ }, z.core.$strip>;
29
+ type GeminiMessageEvent = z.infer<typeof GeminiMessageEvent>;
30
+ /** Tool use event - when the model invokes a tool */
31
+ declare const GeminiToolUseEvent: z.ZodObject<{
32
+ type: z.ZodLiteral<"tool_use">;
33
+ tool_name: z.ZodString;
34
+ tool_id: z.ZodString;
35
+ parameters: z.ZodRecord<z.ZodString, z.ZodUnknown>;
36
+ timestamp: z.ZodString;
37
+ }, z.core.$strip>;
38
+ type GeminiToolUseEvent = z.infer<typeof GeminiToolUseEvent>;
39
+ /** Tool result event - result from a tool execution */
40
+ declare const GeminiToolResultEvent: z.ZodObject<{
41
+ type: z.ZodLiteral<"tool_result">;
42
+ tool_id: z.ZodString;
43
+ status: z.ZodEnum<{
44
+ success: "success";
45
+ error: "error";
46
+ }>;
47
+ output: z.ZodOptional<z.ZodString>;
48
+ error: z.ZodOptional<z.ZodObject<{
49
+ type: z.ZodString;
50
+ message: z.ZodString;
51
+ }, z.core.$strip>>;
52
+ timestamp: z.ZodString;
53
+ }, z.core.$strip>;
54
+ type GeminiToolResultEvent = z.infer<typeof GeminiToolResultEvent>;
55
+ /** Error event - warnings or errors during execution */
56
+ declare const GeminiErrorEvent: z.ZodObject<{
57
+ type: z.ZodLiteral<"error">;
58
+ severity: z.ZodEnum<{
59
+ error: "error";
60
+ warning: "warning";
61
+ }>;
62
+ message: z.ZodString;
63
+ timestamp: z.ZodString;
64
+ }, z.core.$strip>;
65
+ type GeminiErrorEvent = z.infer<typeof GeminiErrorEvent>;
66
+ /** Result event - emitted when task completes */
67
+ declare const GeminiResultEvent: z.ZodObject<{
68
+ type: z.ZodLiteral<"result">;
69
+ status: z.ZodEnum<{
70
+ success: "success";
71
+ error: "error";
72
+ }>;
73
+ error: z.ZodOptional<z.ZodObject<{
74
+ type: z.ZodString;
75
+ message: z.ZodString;
76
+ }, z.core.$strip>>;
77
+ stats: z.ZodOptional<z.ZodObject<{
78
+ total_tokens: z.ZodNumber;
79
+ input_tokens: z.ZodNumber;
80
+ output_tokens: z.ZodNumber;
81
+ duration_ms: z.ZodNumber;
82
+ tool_calls: z.ZodNumber;
83
+ }, z.core.$strip>>;
84
+ timestamp: z.ZodString;
85
+ }, z.core.$strip>;
86
+ type GeminiResultEvent = z.infer<typeof GeminiResultEvent>;
87
+ /** Union of all Gemini CLI event types */
88
+ declare const GeminiEvent: z.ZodDiscriminatedUnion<[z.ZodObject<{
89
+ type: z.ZodLiteral<"init">;
90
+ timestamp: z.ZodString;
91
+ session_id: z.ZodString;
92
+ model: z.ZodOptional<z.ZodString>;
93
+ }, z.core.$strip>, z.ZodObject<{
94
+ type: z.ZodLiteral<"message">;
95
+ role: z.ZodEnum<{
96
+ assistant: "assistant";
97
+ user: "user";
98
+ }>;
99
+ content: z.ZodString;
100
+ delta: z.ZodOptional<z.ZodBoolean>;
101
+ timestamp: z.ZodString;
102
+ }, z.core.$strip>, z.ZodObject<{
103
+ type: z.ZodLiteral<"tool_use">;
104
+ tool_name: z.ZodString;
105
+ tool_id: z.ZodString;
106
+ parameters: z.ZodRecord<z.ZodString, z.ZodUnknown>;
107
+ timestamp: z.ZodString;
108
+ }, z.core.$strip>, z.ZodObject<{
109
+ type: z.ZodLiteral<"tool_result">;
110
+ tool_id: z.ZodString;
111
+ status: z.ZodEnum<{
112
+ success: "success";
113
+ error: "error";
114
+ }>;
115
+ output: z.ZodOptional<z.ZodString>;
116
+ error: z.ZodOptional<z.ZodObject<{
117
+ type: z.ZodString;
118
+ message: z.ZodString;
119
+ }, z.core.$strip>>;
120
+ timestamp: z.ZodString;
121
+ }, z.core.$strip>, z.ZodObject<{
122
+ type: z.ZodLiteral<"error">;
123
+ severity: z.ZodEnum<{
124
+ error: "error";
125
+ warning: "warning";
126
+ }>;
127
+ message: z.ZodString;
128
+ timestamp: z.ZodString;
129
+ }, z.core.$strip>, z.ZodObject<{
130
+ type: z.ZodLiteral<"result">;
131
+ status: z.ZodEnum<{
132
+ success: "success";
133
+ error: "error";
134
+ }>;
135
+ error: z.ZodOptional<z.ZodObject<{
136
+ type: z.ZodString;
137
+ message: z.ZodString;
138
+ }, z.core.$strip>>;
139
+ stats: z.ZodOptional<z.ZodObject<{
140
+ total_tokens: z.ZodNumber;
141
+ input_tokens: z.ZodNumber;
142
+ output_tokens: z.ZodNumber;
143
+ duration_ms: z.ZodNumber;
144
+ tool_calls: z.ZodNumber;
145
+ }, z.core.$strip>>;
146
+ timestamp: z.ZodString;
147
+ }, z.core.$strip>], "type">;
148
+ type GeminiEvent = z.infer<typeof GeminiEvent>;
149
+ /** Type guard for init events */
150
+ declare function isInitEvent(event: GeminiEvent): event is GeminiInitEvent;
151
+ /** Type guard for message events */
152
+ declare function isMessageEvent(event: GeminiEvent): event is GeminiMessageEvent;
153
+ /** Type guard for tool use events */
154
+ declare function isToolUseEvent(event: GeminiEvent): event is GeminiToolUseEvent;
155
+ /** Type guard for tool result events */
156
+ declare function isToolResultEvent(event: GeminiEvent): event is GeminiToolResultEvent;
157
+ /** Type guard for error events */
158
+ declare function isErrorEvent(event: GeminiEvent): event is GeminiErrorEvent;
159
+ /** Type guard for result events */
160
+ declare function isResultEvent(event: GeminiEvent): event is GeminiResultEvent;
161
+ export { GeminiEvent, isErrorEvent, isInitEvent, isMessageEvent, isResultEvent, isToolResultEvent, isToolUseEvent, };
162
+ export type { GeminiErrorEvent, GeminiInitEvent, GeminiMessageEvent, GeminiResultEvent, GeminiToolResultEvent, GeminiToolUseEvent, };
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Raw event types from Gemini CLI's JSONL stream.
3
+ *
4
+ * These types represent the raw output from:
5
+ * `gemini --output-format stream-json "..."`
6
+ *
7
+ * The parser transforms these into normalized {@link AxexecEvent} types.
8
+ */
9
+ import { z } from "zod";
10
+ /** Init event - emitted at startup with session info */
11
+ const GeminiInitEvent = z.object({
12
+ type: z.literal("init"),
13
+ timestamp: z.string(),
14
+ session_id: z.string(),
15
+ model: z.string().optional(),
16
+ });
17
+ /** Message event - user or assistant messages */
18
+ const GeminiMessageEvent = z.object({
19
+ type: z.literal("message"),
20
+ role: z.enum(["user", "assistant"]),
21
+ content: z.string(),
22
+ delta: z.boolean().optional(),
23
+ timestamp: z.string(),
24
+ });
25
+ /** Tool use event - when the model invokes a tool */
26
+ const GeminiToolUseEvent = z.object({
27
+ type: z.literal("tool_use"),
28
+ tool_name: z.string(),
29
+ tool_id: z.string(),
30
+ parameters: z.record(z.string(), z.unknown()),
31
+ timestamp: z.string(),
32
+ });
33
+ /** Error info schema - used in tool_result and result events */
34
+ const GeminiErrorInfo = z.object({
35
+ type: z.string(),
36
+ message: z.string(),
37
+ });
38
+ /** Tool result event - result from a tool execution */
39
+ const GeminiToolResultEvent = z.object({
40
+ type: z.literal("tool_result"),
41
+ tool_id: z.string(),
42
+ status: z.enum(["success", "error"]),
43
+ output: z.string().optional(),
44
+ error: GeminiErrorInfo.optional(),
45
+ timestamp: z.string(),
46
+ });
47
+ /** Error event - warnings or errors during execution */
48
+ const GeminiErrorEvent = z.object({
49
+ type: z.literal("error"),
50
+ severity: z.enum(["warning", "error"]),
51
+ message: z.string(),
52
+ timestamp: z.string(),
53
+ });
54
+ /** Stats schema - used in result events */
55
+ const GeminiStats = z.object({
56
+ total_tokens: z.number(),
57
+ input_tokens: z.number(),
58
+ output_tokens: z.number(),
59
+ duration_ms: z.number(),
60
+ tool_calls: z.number(),
61
+ });
62
+ /** Result event - emitted when task completes */
63
+ const GeminiResultEvent = z.object({
64
+ type: z.literal("result"),
65
+ status: z.enum(["success", "error"]),
66
+ error: GeminiErrorInfo.optional(),
67
+ stats: GeminiStats.optional(),
68
+ timestamp: z.string(),
69
+ });
70
+ /** Union of all Gemini CLI event types */
71
+ const GeminiEvent = z.discriminatedUnion("type", [
72
+ GeminiInitEvent,
73
+ GeminiMessageEvent,
74
+ GeminiToolUseEvent,
75
+ GeminiToolResultEvent,
76
+ GeminiErrorEvent,
77
+ GeminiResultEvent,
78
+ ]);
79
+ /** Type guard for init events */
80
+ function isInitEvent(event) {
81
+ return event.type === "init";
82
+ }
83
+ /** Type guard for message events */
84
+ function isMessageEvent(event) {
85
+ return event.type === "message";
86
+ }
87
+ /** Type guard for tool use events */
88
+ function isToolUseEvent(event) {
89
+ return event.type === "tool_use";
90
+ }
91
+ /** Type guard for tool result events */
92
+ function isToolResultEvent(event) {
93
+ return event.type === "tool_result";
94
+ }
95
+ /** Type guard for error events */
96
+ function isErrorEvent(event) {
97
+ return event.type === "error";
98
+ }
99
+ /** Type guard for result events */
100
+ function isResultEvent(event) {
101
+ return event.type === "result";
102
+ }
103
+ export { GeminiEvent, isErrorEvent, isInitEvent, isMessageEvent, isResultEvent, isToolResultEvent, isToolUseEvent, };