mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
feat: add session_id output to enable resuming conversations (#739)
Add a new `session_id` output that exposes the Claude Code session ID, allowing other workflows or Claude Code instances to resume the conversation using `--resume <session_id>`. Changes: - Add parseAndSetSessionId() function to extract session_id from the system.init message in execution output - Add session_id output to both action.yml and base-action/action.yml - Add comprehensive tests for the new functionality 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -127,6 +127,9 @@ outputs:
|
|||||||
structured_output:
|
structured_output:
|
||||||
description: "JSON string containing all structured output fields when --json-schema is provided in claude_args. Use fromJSON() to parse: fromJSON(steps.id.outputs.structured_output).field_name"
|
description: "JSON string containing all structured output fields when --json-schema is provided in claude_args. Use fromJSON() to parse: fromJSON(steps.id.outputs.structured_output).field_name"
|
||||||
value: ${{ steps.claude-code.outputs.structured_output }}
|
value: ${{ steps.claude-code.outputs.structured_output }}
|
||||||
|
session_id:
|
||||||
|
description: "The Claude Code session ID that can be used with --resume to continue this conversation"
|
||||||
|
value: ${{ steps.claude-code.outputs.session_id }}
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ outputs:
|
|||||||
structured_output:
|
structured_output:
|
||||||
description: "JSON string containing all structured output fields when --json-schema is provided in claude_args (use fromJSON() or jq to parse)"
|
description: "JSON string containing all structured output fields when --json-schema is provided in claude_args (use fromJSON() or jq to parse)"
|
||||||
value: ${{ steps.run_claude.outputs.structured_output }}
|
value: ${{ steps.run_claude.outputs.structured_output }}
|
||||||
|
session_id:
|
||||||
|
description: "The Claude Code session ID that can be used with --resume to continue this conversation"
|
||||||
|
value: ${{ steps.run_claude.outputs.session_id }}
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
|
|||||||
@@ -124,6 +124,36 @@ export function prepareRunConfig(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses session_id from execution file and sets GitHub Action output
|
||||||
|
* Exported for testing
|
||||||
|
*/
|
||||||
|
export async function parseAndSetSessionId(
|
||||||
|
executionFile: string,
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const content = await readFile(executionFile, "utf-8");
|
||||||
|
const messages = JSON.parse(content) as {
|
||||||
|
type: string;
|
||||||
|
subtype?: string;
|
||||||
|
session_id?: string;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
// Find the system.init message which contains session_id
|
||||||
|
const initMessage = messages.find(
|
||||||
|
(m) => m.type === "system" && m.subtype === "init",
|
||||||
|
);
|
||||||
|
|
||||||
|
if (initMessage?.session_id) {
|
||||||
|
core.setOutput("session_id", initMessage.session_id);
|
||||||
|
core.info(`Set session_id: ${initMessage.session_id}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Don't fail the action if session_id extraction fails
|
||||||
|
core.warning(`Failed to extract session_id: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses structured_output from execution file and sets GitHub Action outputs
|
* Parses structured_output from execution file and sets GitHub Action outputs
|
||||||
* Only runs if --json-schema was explicitly provided in claude_args
|
* Only runs if --json-schema was explicitly provided in claude_args
|
||||||
@@ -368,6 +398,9 @@ export async function runClaude(promptPath: string, options: ClaudeOptions) {
|
|||||||
|
|
||||||
core.setOutput("execution_file", EXECUTION_FILE);
|
core.setOutput("execution_file", EXECUTION_FILE);
|
||||||
|
|
||||||
|
// Extract and set session_id
|
||||||
|
await parseAndSetSessionId(EXECUTION_FILE);
|
||||||
|
|
||||||
// Parse and set structured outputs only if user provided --json-schema in claude_args
|
// Parse and set structured outputs only if user provided --json-schema in claude_args
|
||||||
if (hasJsonSchema) {
|
if (hasJsonSchema) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import { describe, test, expect, afterEach, beforeEach, spyOn } from "bun:test";
|
|||||||
import { writeFile, unlink } from "fs/promises";
|
import { writeFile, unlink } from "fs/promises";
|
||||||
import { tmpdir } from "os";
|
import { tmpdir } from "os";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { parseAndSetStructuredOutputs } from "../src/run-claude";
|
import {
|
||||||
|
parseAndSetStructuredOutputs,
|
||||||
|
parseAndSetSessionId,
|
||||||
|
} from "../src/run-claude";
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
|
||||||
// Mock execution file path
|
// Mock execution file path
|
||||||
@@ -35,16 +38,19 @@ async function createMockExecutionFile(
|
|||||||
// Spy on core functions
|
// Spy on core functions
|
||||||
let setOutputSpy: any;
|
let setOutputSpy: any;
|
||||||
let infoSpy: any;
|
let infoSpy: any;
|
||||||
|
let warningSpy: any;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
setOutputSpy = spyOn(core, "setOutput").mockImplementation(() => {});
|
setOutputSpy = spyOn(core, "setOutput").mockImplementation(() => {});
|
||||||
infoSpy = spyOn(core, "info").mockImplementation(() => {});
|
infoSpy = spyOn(core, "info").mockImplementation(() => {});
|
||||||
|
warningSpy = spyOn(core, "warning").mockImplementation(() => {});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("parseAndSetStructuredOutputs", () => {
|
describe("parseAndSetStructuredOutputs", () => {
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
setOutputSpy?.mockRestore();
|
setOutputSpy?.mockRestore();
|
||||||
infoSpy?.mockRestore();
|
infoSpy?.mockRestore();
|
||||||
|
warningSpy?.mockRestore();
|
||||||
try {
|
try {
|
||||||
await unlink(TEST_EXECUTION_FILE);
|
await unlink(TEST_EXECUTION_FILE);
|
||||||
} catch {
|
} catch {
|
||||||
@@ -156,3 +162,66 @@ describe("parseAndSetStructuredOutputs", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("parseAndSetSessionId", () => {
|
||||||
|
afterEach(async () => {
|
||||||
|
setOutputSpy?.mockRestore();
|
||||||
|
infoSpy?.mockRestore();
|
||||||
|
warningSpy?.mockRestore();
|
||||||
|
try {
|
||||||
|
await unlink(TEST_EXECUTION_FILE);
|
||||||
|
} catch {
|
||||||
|
// Ignore if file doesn't exist
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should extract session_id from system.init message", async () => {
|
||||||
|
const messages = [
|
||||||
|
{ type: "system", subtype: "init", session_id: "test-session-123" },
|
||||||
|
{ type: "result", cost_usd: 0.01 },
|
||||||
|
];
|
||||||
|
await writeFile(TEST_EXECUTION_FILE, JSON.stringify(messages));
|
||||||
|
|
||||||
|
await parseAndSetSessionId(TEST_EXECUTION_FILE);
|
||||||
|
|
||||||
|
expect(setOutputSpy).toHaveBeenCalledWith("session_id", "test-session-123");
|
||||||
|
expect(infoSpy).toHaveBeenCalledWith("Set session_id: test-session-123");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should handle missing session_id gracefully", async () => {
|
||||||
|
const messages = [
|
||||||
|
{ type: "system", subtype: "init" },
|
||||||
|
{ type: "result", cost_usd: 0.01 },
|
||||||
|
];
|
||||||
|
await writeFile(TEST_EXECUTION_FILE, JSON.stringify(messages));
|
||||||
|
|
||||||
|
await parseAndSetSessionId(TEST_EXECUTION_FILE);
|
||||||
|
|
||||||
|
expect(setOutputSpy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should handle missing system.init message gracefully", async () => {
|
||||||
|
const messages = [{ type: "result", cost_usd: 0.01 }];
|
||||||
|
await writeFile(TEST_EXECUTION_FILE, JSON.stringify(messages));
|
||||||
|
|
||||||
|
await parseAndSetSessionId(TEST_EXECUTION_FILE);
|
||||||
|
|
||||||
|
expect(setOutputSpy).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should handle malformed JSON gracefully with warning", async () => {
|
||||||
|
await writeFile(TEST_EXECUTION_FILE, "{ invalid json");
|
||||||
|
|
||||||
|
await parseAndSetSessionId(TEST_EXECUTION_FILE);
|
||||||
|
|
||||||
|
expect(setOutputSpy).not.toHaveBeenCalled();
|
||||||
|
expect(warningSpy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should handle non-existent file gracefully with warning", async () => {
|
||||||
|
await parseAndSetSessionId("/nonexistent/file.json");
|
||||||
|
|
||||||
|
expect(setOutputSpy).not.toHaveBeenCalled();
|
||||||
|
expect(warningSpy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user