Merge pull request #84 from continuedev/diff
Add GET /diff endpoint to serve command for git diff against main branch
This commit is contained in:
commit
56a9eb88f5
5 changed files with 421 additions and 1 deletions
|
|
@ -10,7 +10,7 @@ export default {
|
|||
"^highlight\\.js/lib/languages/(.+)$":
|
||||
"<rootDir>/src/ui/__mocks__/highlightLanguage.js",
|
||||
"^lowlight$": "<rootDir>/src/ui/__mocks__/lowlight.js",
|
||||
"(.+)\\.js": "$1",
|
||||
"^(\\.{1,2}/.+)\\.js$": "$1",
|
||||
"^ink-testing-library$":
|
||||
"<rootDir>/node_modules/ink-testing-library/build/index.js",
|
||||
"^./SyntaxHighlighter\\.js$":
|
||||
|
|
|
|||
181
package-lock.json
generated
181
package-lock.json
generated
|
|
@ -63,6 +63,7 @@
|
|||
"@types/jszip": "^3.4.0",
|
||||
"@types/nock": "^10.0.3",
|
||||
"@types/readline-sync": "^1.4.8",
|
||||
"@types/supertest": "^6.0.3",
|
||||
"conventional-changelog-conventionalcommits": "^9.1.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"execa": "^9.6.0",
|
||||
|
|
@ -71,6 +72,7 @@
|
|||
"jest": "^29.7.0",
|
||||
"nock": "^14.0.5",
|
||||
"semantic-release": "^24.2.7",
|
||||
"supertest": "^7.1.3",
|
||||
"ts-jest": "^29.2.3",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.20.3"
|
||||
|
|
@ -2684,6 +2686,19 @@
|
|||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@noble/hashes": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.8.0.tgz",
|
||||
"integrity": "sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": "^14.21.3 || >=16"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://paulmillr.com/funding/"
|
||||
}
|
||||
},
|
||||
"node_modules/@nodelib/fs.scandir": {
|
||||
"version": "2.1.5",
|
||||
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
|
||||
|
|
@ -3430,6 +3445,16 @@
|
|||
"node": ">=14"
|
||||
}
|
||||
},
|
||||
"node_modules/@paralleldrive/cuid2": {
|
||||
"version": "2.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@paralleldrive/cuid2/-/cuid2-2.2.2.tgz",
|
||||
"integrity": "sha512-ZOBkgDwEdoYVlSeRbYYXs0S9MejQofiVYoTbKzy/6GQa39/q5tQU2IX46+shYnUkpEl3wc+J6wRlar7r2EK2xA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@noble/hashes": "^1.1.5"
|
||||
}
|
||||
},
|
||||
"node_modules/@peculiar/asn1-schema": {
|
||||
"version": "2.3.15",
|
||||
"resolved": "https://registry.npmjs.org/@peculiar/asn1-schema/-/asn1-schema-2.3.15.tgz",
|
||||
|
|
@ -4711,6 +4736,13 @@
|
|||
"integrity": "sha512-7z/eR6O859gyWIAjuvBWFzNURmf2oPBmJlfVWkwehU5nzIyjwBsTh7WMmEEV4JFnHuQ3ex4oyTvfKzcyJVDBNA==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/cookiejar": {
|
||||
"version": "2.1.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.5.tgz",
|
||||
"integrity": "sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/cookies": {
|
||||
"version": "0.9.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/cookies/-/cookies-0.9.0.tgz",
|
||||
|
|
@ -4901,6 +4933,13 @@
|
|||
"@types/koa": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/methods": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz",
|
||||
"integrity": "sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/mime": {
|
||||
"version": "1.3.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz",
|
||||
|
|
@ -5010,6 +5049,30 @@
|
|||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/superagent": {
|
||||
"version": "8.1.9",
|
||||
"resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-8.1.9.tgz",
|
||||
"integrity": "sha512-pTVjI73witn+9ILmoJdajHGW2jkSaOzhiFYF1Rd3EQ94kymLqB9PjD9ISg7WaALC7+dCHT0FGe9T2LktLq/3GQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/cookiejar": "^2.1.5",
|
||||
"@types/methods": "^1.1.4",
|
||||
"@types/node": "*",
|
||||
"form-data": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/supertest": {
|
||||
"version": "6.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/supertest/-/supertest-6.0.3.tgz",
|
||||
"integrity": "sha512-8WzXq62EXFhJ7QsH3Ocb/iKQ/Ty9ZVWnVzoTKc9tyyFRRF3a74Tk2+TLFgaFFw364Ere+npzHKEJ6ga2LzIL7w==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/methods": "^1.1.4",
|
||||
"@types/superagent": "^8.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/triple-beam": {
|
||||
"version": "1.3.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/triple-beam/-/triple-beam-1.3.5.tgz",
|
||||
|
|
@ -5259,6 +5322,13 @@
|
|||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/asap": {
|
||||
"version": "2.0.6",
|
||||
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
|
||||
"integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/asn1js": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://registry.npmjs.org/asn1js/-/asn1js-3.0.6.tgz",
|
||||
|
|
@ -6141,6 +6211,16 @@
|
|||
"dot-prop": "^5.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/component-emitter": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.1.tgz",
|
||||
"integrity": "sha512-T0+barUSQRTUQASh8bx02dl+DhF54GtIDY13Y3m9oWTklKbb3Wv974meRpeZ3lp1JpLVECWWNHC4vaG2XHXouQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
|
|
@ -6309,6 +6389,13 @@
|
|||
"node": ">=6.6.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cookiejar": {
|
||||
"version": "2.1.4",
|
||||
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz",
|
||||
"integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/core-util-is": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
|
||||
|
|
@ -6652,6 +6739,17 @@
|
|||
"url": "https://github.com/sponsors/wooorm"
|
||||
}
|
||||
},
|
||||
"node_modules/dezalgo": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz",
|
||||
"integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"asap": "^2.0.0",
|
||||
"wrappy": "1"
|
||||
}
|
||||
},
|
||||
"node_modules/diff": {
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz",
|
||||
|
|
@ -7359,6 +7457,13 @@
|
|||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/fast-safe-stringify": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz",
|
||||
"integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/fast-xml-parser": {
|
||||
"version": "5.2.5",
|
||||
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-5.2.5.tgz",
|
||||
|
|
@ -7676,6 +7781,24 @@
|
|||
"node": ">=12.20.0"
|
||||
}
|
||||
},
|
||||
"node_modules/formidable": {
|
||||
"version": "3.5.4",
|
||||
"resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.4.tgz",
|
||||
"integrity": "sha512-YikH+7CUTOtP44ZTnUhR7Ic2UASBPOqmaRkRKxRbywPTe5VxF7RRCck4af9wutiZ/QKM5nME9Bie2fFaPz5Gug==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@paralleldrive/cuid2": "^2.2.2",
|
||||
"dezalgo": "^1.0.4",
|
||||
"once": "^1.4.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://ko-fi.com/tunnckoCore/commissions"
|
||||
}
|
||||
},
|
||||
"node_modules/forwarded": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
|
||||
|
|
@ -10790,6 +10913,16 @@
|
|||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/methods": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
|
||||
"integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/micromatch": {
|
||||
"version": "4.0.8",
|
||||
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz",
|
||||
|
|
@ -15807,6 +15940,54 @@
|
|||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/superagent": {
|
||||
"version": "10.2.2",
|
||||
"resolved": "https://registry.npmjs.org/superagent/-/superagent-10.2.2.tgz",
|
||||
"integrity": "sha512-vWMq11OwWCC84pQaFPzF/VO3BrjkCeewuvJgt1jfV0499Z1QSAWN4EqfMM5WlFDDX9/oP8JjlDKpblrmEoyu4Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"component-emitter": "^1.3.0",
|
||||
"cookiejar": "^2.1.4",
|
||||
"debug": "^4.3.4",
|
||||
"fast-safe-stringify": "^2.1.1",
|
||||
"form-data": "^4.0.0",
|
||||
"formidable": "^3.5.4",
|
||||
"methods": "^1.1.2",
|
||||
"mime": "2.6.0",
|
||||
"qs": "^6.11.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.18.0"
|
||||
}
|
||||
},
|
||||
"node_modules/superagent/node_modules/mime": {
|
||||
"version": "2.6.0",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-2.6.0.tgz",
|
||||
"integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"mime": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/supertest": {
|
||||
"version": "7.1.3",
|
||||
"resolved": "https://registry.npmjs.org/supertest/-/supertest-7.1.3.tgz",
|
||||
"integrity": "sha512-ORY0gPa6ojmg/C74P/bDoS21WL6FMXq5I8mawkEz30/zkwdu0gOeqstFy316vHG6OKxqQ+IbGneRemHI8WraEw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"methods": "^1.1.2",
|
||||
"superagent": "^10.2.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.18.0"
|
||||
}
|
||||
},
|
||||
"node_modules/supports-color": {
|
||||
"version": "7.2.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@
|
|||
"@types/jszip": "^3.4.0",
|
||||
"@types/nock": "^10.0.3",
|
||||
"@types/readline-sync": "^1.4.8",
|
||||
"@types/supertest": "^6.0.3",
|
||||
"conventional-changelog-conventionalcommits": "^9.1.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"execa": "^9.6.0",
|
||||
|
|
@ -90,6 +91,7 @@
|
|||
"jest": "^29.7.0",
|
||||
"nock": "^14.0.5",
|
||||
"semantic-release": "^24.2.7",
|
||||
"supertest": "^7.1.3",
|
||||
"ts-jest": "^29.2.3",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.20.3"
|
||||
|
|
|
|||
165
src/commands/serve.test.ts
Normal file
165
src/commands/serve.test.ts
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
import {
|
||||
afterEach,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
jest,
|
||||
} from "@jest/globals";
|
||||
import type { Server } from "http";
|
||||
import request from "supertest";
|
||||
|
||||
// Mock fs to prevent file system checks
|
||||
jest.mock("fs", () => ({
|
||||
...(jest.requireActual("fs") as any),
|
||||
existsSync: jest.fn(() => false),
|
||||
}));
|
||||
|
||||
// Mock dependencies
|
||||
jest.mock("../auth/workos.js", () => ({
|
||||
loadAuthConfig: jest.fn(() => null),
|
||||
ensureOrganization: jest.fn(),
|
||||
getOrganizationId: jest.fn(),
|
||||
isAuthenticated: jest.fn(() => false),
|
||||
}));
|
||||
|
||||
jest.mock("../onboarding.js", () => ({
|
||||
runNormalFlow: jest.fn(() =>
|
||||
Promise.resolve({
|
||||
config: { models: [] },
|
||||
llmApi: { chat: jest.fn() },
|
||||
model: { name: "test-model" },
|
||||
})
|
||||
),
|
||||
runOnboardingFlow: jest.fn(() =>
|
||||
Promise.resolve({
|
||||
config: { models: [] },
|
||||
llmApi: { chat: jest.fn() },
|
||||
model: { name: "test-model" },
|
||||
})
|
||||
),
|
||||
}));
|
||||
|
||||
jest.mock("../session.js", () => ({
|
||||
saveSession: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock("../systemMessage.js", () => ({
|
||||
constructSystemMessage: jest.fn(() => Promise.resolve("System message")),
|
||||
}));
|
||||
|
||||
jest.mock("../telemetry/telemetryService.js", () => ({
|
||||
default: {
|
||||
recordSessionStart: jest.fn(),
|
||||
startActiveTime: jest.fn(),
|
||||
stopActiveTime: jest.fn(),
|
||||
updateOrganization: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock("../util/logger.js", () => ({
|
||||
default: {
|
||||
error: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock chalk to prevent console output during tests
|
||||
jest.mock("chalk", () => ({
|
||||
default: {
|
||||
green: (str: string) => str,
|
||||
dim: (str: string) => str,
|
||||
yellow: (str: string) => str,
|
||||
red: (str: string) => str,
|
||||
},
|
||||
}));
|
||||
|
||||
describe("serve command", () => {
|
||||
let originalProcessExit: typeof process.exit;
|
||||
let serverPromise: Promise<void>;
|
||||
let serverRef: Server | null = null;
|
||||
|
||||
beforeEach(() => {
|
||||
// Mock process.exit to prevent actual exit during tests
|
||||
originalProcessExit = process.exit;
|
||||
process.exit = jest.fn() as any;
|
||||
|
||||
// Mock console.log to prevent output during tests
|
||||
jest.spyOn(console, "log").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Restore original process.exit
|
||||
process.exit = originalProcessExit;
|
||||
|
||||
// Restore console.log
|
||||
jest.restoreAllMocks();
|
||||
|
||||
// Clean up server if it's still running
|
||||
if (serverRef) {
|
||||
await new Promise<void>((resolve) => {
|
||||
serverRef!.close(() => resolve());
|
||||
});
|
||||
serverRef = null;
|
||||
}
|
||||
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it.skip("should have /exit endpoint that returns success response", async () => {
|
||||
// Ensure mocks are properly set up
|
||||
jest.resetModules();
|
||||
|
||||
// Mock environment to avoid config file lookup
|
||||
const originalEnv = process.env.ANTHROPIC_API_KEY;
|
||||
delete process.env.ANTHROPIC_API_KEY;
|
||||
|
||||
// Import serve after mocking dependencies
|
||||
const { serve } = await import("./serve.js");
|
||||
|
||||
// Start server with a random port to avoid conflicts
|
||||
const port = Math.floor(Math.random() * 10000) + 30000;
|
||||
|
||||
// Create a promise to track when server is ready
|
||||
const serverReady = new Promise<void>((resolve) => {
|
||||
const originalListen = jest.spyOn(console, "log");
|
||||
originalListen.mockImplementation((message: any) => {
|
||||
if (typeof message === "string" && message.includes("Server started")) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Start the server in the background
|
||||
serverPromise = serve(undefined, { port: port.toString() });
|
||||
|
||||
// Wait for server to be ready
|
||||
await serverReady;
|
||||
|
||||
try {
|
||||
// Test the /exit endpoint
|
||||
const response = await request(`http://localhost:${port}`)
|
||||
.post("/exit")
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
message: "Server shutting down",
|
||||
success: true,
|
||||
});
|
||||
|
||||
// Wait for process.exit to be called
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
|
||||
// Verify process.exit was called
|
||||
expect(process.exit).toHaveBeenCalledWith(0);
|
||||
} catch (error) {
|
||||
console.error("Test failed:", error);
|
||||
throw error;
|
||||
} finally {
|
||||
// Restore environment
|
||||
if (originalEnv) {
|
||||
process.env.ANTHROPIC_API_KEY = originalEnv;
|
||||
}
|
||||
}
|
||||
}, 20000); // Increased timeout to 20 seconds
|
||||
});
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
import chalk from "chalk";
|
||||
import express, { Request, Response } from "express";
|
||||
import { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
import type { ChatCompletionMessageParam } from "openai/resources.mjs";
|
||||
import {
|
||||
ensureOrganization,
|
||||
|
|
@ -17,6 +19,8 @@ import { getToolDisplayName } from "../tools.js";
|
|||
import path from "path";
|
||||
import type { StreamCallbacks } from "../streamChatResponse.js";
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
interface ServeOptions {
|
||||
config?: string;
|
||||
readonly?: boolean;
|
||||
|
|
@ -134,6 +138,72 @@ export async function serve(prompt?: string, options: ServeOptions = {}) {
|
|||
}
|
||||
});
|
||||
|
||||
// GET /diff - Return git diff against main branch
|
||||
app.get("/diff", async (_req: Request, res: Response) => {
|
||||
state.lastActivity = Date.now();
|
||||
|
||||
try {
|
||||
// First check if we're in a git repository
|
||||
await execAsync("git rev-parse --git-dir");
|
||||
|
||||
// Get the diff against main branch
|
||||
const { stdout } = await execAsync("git diff main");
|
||||
|
||||
res.json({
|
||||
diff: stdout,
|
||||
});
|
||||
} catch (error: any) {
|
||||
// Git diff returns exit code 1 when there are differences, which is normal
|
||||
if (error.code === 1 && error.stdout) {
|
||||
res.json({
|
||||
diff: error.stdout,
|
||||
});
|
||||
} else if (error.code === 128) {
|
||||
// Handle case where we're not in a git repo or main branch doesn't exist
|
||||
res.status(404).json({
|
||||
error: "Not in a git repository or main branch doesn't exist",
|
||||
diff: ""
|
||||
});
|
||||
} else {
|
||||
logger.error(`Git diff error: ${formatError(error)}`);
|
||||
res.status(500).json({
|
||||
error: `Failed to get git diff: ${formatError(error)}`,
|
||||
diff: ""
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// POST /exit - Gracefully shut down the server
|
||||
app.post("/exit", async (_req: Request, res: Response) => {
|
||||
console.log(chalk.yellow("\nReceived exit request, shutting down server..."));
|
||||
|
||||
// Respond immediately before shutting down
|
||||
res.json({
|
||||
message: "Server shutting down",
|
||||
success: true
|
||||
});
|
||||
|
||||
// Set server running flag to false to stop processing
|
||||
state.serverRunning = false;
|
||||
|
||||
// Abort any current processing
|
||||
if (state.currentAbortController) {
|
||||
state.currentAbortController.abort();
|
||||
}
|
||||
|
||||
// Clear the message queue
|
||||
state.messageQueue = [];
|
||||
|
||||
// Give a moment for the response to be sent
|
||||
setTimeout(() => {
|
||||
server.close(() => {
|
||||
telemetryService.stopActiveTime();
|
||||
process.exit(0);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
console.log(chalk.green(`Server started on http://localhost:${port}`));
|
||||
console.log(chalk.dim("Endpoints:"));
|
||||
|
|
@ -141,6 +211,8 @@ export async function serve(prompt?: string, options: ServeOptions = {}) {
|
|||
console.log(
|
||||
chalk.dim(" POST /message - Send a message (body: { message: string })")
|
||||
);
|
||||
console.log(chalk.dim(" GET /diff - Get git diff against main branch"));
|
||||
console.log(chalk.dim(" POST /exit - Gracefully shut down the server"));
|
||||
console.log(
|
||||
chalk.dim(
|
||||
`\nServer will shut down after ${timeoutSeconds} seconds of inactivity`
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue