@fronx/use-claude-code 0.1.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 (58) hide show
  1. package/README.md +273 -0
  2. package/dist/client/connection.d.ts +101 -0
  3. package/dist/client/connection.d.ts.map +1 -0
  4. package/dist/client/connection.js +379 -0
  5. package/dist/client/connection.js.map +1 -0
  6. package/dist/client/event-emitter.d.ts +32 -0
  7. package/dist/client/event-emitter.d.ts.map +1 -0
  8. package/dist/client/event-emitter.js +72 -0
  9. package/dist/client/event-emitter.js.map +1 -0
  10. package/dist/client/index.d.ts +30 -0
  11. package/dist/client/index.d.ts.map +1 -0
  12. package/dist/client/index.js +29 -0
  13. package/dist/client/index.js.map +1 -0
  14. package/dist/client/state-machine.d.ts +61 -0
  15. package/dist/client/state-machine.d.ts.map +1 -0
  16. package/dist/client/state-machine.js +292 -0
  17. package/dist/client/state-machine.js.map +1 -0
  18. package/dist/client/stream-processor.d.ts +46 -0
  19. package/dist/client/stream-processor.d.ts.map +1 -0
  20. package/dist/client/stream-processor.js +173 -0
  21. package/dist/client/stream-processor.js.map +1 -0
  22. package/dist/react/hook.d.ts +55 -0
  23. package/dist/react/hook.d.ts.map +1 -0
  24. package/dist/react/hook.js +132 -0
  25. package/dist/react/hook.js.map +1 -0
  26. package/dist/react/index.d.ts +49 -0
  27. package/dist/react/index.d.ts.map +1 -0
  28. package/dist/react/index.js +48 -0
  29. package/dist/react/index.js.map +1 -0
  30. package/dist/server/claude-spawn.d.ts +52 -0
  31. package/dist/server/claude-spawn.d.ts.map +1 -0
  32. package/dist/server/claude-spawn.js +147 -0
  33. package/dist/server/claude-spawn.js.map +1 -0
  34. package/dist/server/conversation.d.ts +113 -0
  35. package/dist/server/conversation.d.ts.map +1 -0
  36. package/dist/server/conversation.js +520 -0
  37. package/dist/server/conversation.js.map +1 -0
  38. package/dist/server/index.d.ts +35 -0
  39. package/dist/server/index.d.ts.map +1 -0
  40. package/dist/server/index.js +39 -0
  41. package/dist/server/index.js.map +1 -0
  42. package/dist/server/sse-emitter.d.ts +42 -0
  43. package/dist/server/sse-emitter.d.ts.map +1 -0
  44. package/dist/server/sse-emitter.js +56 -0
  45. package/dist/server/sse-emitter.js.map +1 -0
  46. package/dist/shared/index.d.ts +3 -0
  47. package/dist/shared/index.d.ts.map +1 -0
  48. package/dist/shared/index.js +3 -0
  49. package/dist/shared/index.js.map +1 -0
  50. package/dist/shared/tool-utils.d.ts +25 -0
  51. package/dist/shared/tool-utils.d.ts.map +1 -0
  52. package/dist/shared/tool-utils.js +49 -0
  53. package/dist/shared/tool-utils.js.map +1 -0
  54. package/dist/shared/types.d.ts +82 -0
  55. package/dist/shared/types.d.ts.map +1 -0
  56. package/dist/shared/types.js +5 -0
  57. package/dist/shared/types.js.map +1 -0
  58. package/package.json +40 -0
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Server-Sent Events (SSE) response formatting helpers.
3
+ *
4
+ * These utilities help format SSE responses according to the specification,
5
+ * with proper event names and JSON data payloads.
6
+ */
7
+ /**
8
+ * Set up an HTTP response for SSE streaming.
9
+ *
10
+ * @param res - The HTTP response object
11
+ */
12
+ export function setupSSEResponse(res) {
13
+ res.setHeader('Content-Type', 'text/event-stream');
14
+ res.setHeader('Cache-Control', 'no-cache');
15
+ res.setHeader('Connection', 'keep-alive');
16
+ }
17
+ /**
18
+ * Send a Server-Sent Event to a single response.
19
+ *
20
+ * @param res - The HTTP response object
21
+ * @param event - The event type (e.g., 'init', 'assistant', 'result')
22
+ * @param data - The event data (will be JSON stringified)
23
+ */
24
+ export function sendSSE(res, event, data) {
25
+ if (!res.destroyed) {
26
+ res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
27
+ }
28
+ }
29
+ /**
30
+ * Broadcast an SSE event to multiple responses.
31
+ *
32
+ * @param responses - Set of HTTP response objects
33
+ * @param event - The event type
34
+ * @param data - The event data (will be JSON stringified)
35
+ */
36
+ export function broadcastSSE(responses, event, data) {
37
+ responses.forEach((res) => {
38
+ sendSSE(res, event, data);
39
+ });
40
+ }
41
+ /**
42
+ * Broadcast an SSE event with a session ID attached.
43
+ *
44
+ * This is important for clients to filter out stale events from previous
45
+ * sessions (e.g., when clear() is called mid-stream).
46
+ *
47
+ * @param responses - Set of HTTP response objects
48
+ * @param event - The event type
49
+ * @param data - The event data (sessionId will be added)
50
+ * @param sessionId - The current session ID
51
+ */
52
+ export function broadcastSSEWithSession(responses, event, data, sessionId) {
53
+ const dataWithSession = { ...data, sessionId };
54
+ broadcastSSE(responses, event, dataWithSession);
55
+ }
56
+ //# sourceMappingURL=sse-emitter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse-emitter.js","sourceRoot":"","sources":["../../src/server/sse-emitter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAmB;IAClD,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,mBAAmB,CAAC,CAAC;IACnD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;IAC3C,GAAG,CAAC,SAAS,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAC,GAAmB,EAAE,KAAa,EAAE,IAAa;IACvE,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC;QACnB,GAAG,CAAC,KAAK,CAAC,UAAU,KAAK,WAAW,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAClE,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAC1B,SAA8B,EAC9B,KAAa,EACb,IAAa;IAEb,SAAS,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACxB,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAC5B,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,uBAAuB,CACrC,SAA8B,EAC9B,KAAa,EACb,IAA6B,EAC7B,SAAiB;IAEjB,MAAM,eAAe,GAAG,EAAE,GAAG,IAAI,EAAE,SAAS,EAAE,CAAC;IAC/C,YAAY,CAAC,SAAS,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC;AAClD,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './tool-utils.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './tool-utils.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Utility functions for extracting human-readable information from tool invocations.
3
+ */
4
+ /**
5
+ * Extract the most relevant parameter from a tool invocation for display.
6
+ *
7
+ * @param toolName - The name of the tool being invoked
8
+ * @param input - The tool's input parameters
9
+ * @returns The most relevant parameter value, or undefined
10
+ *
11
+ * @example
12
+ * getRelevantToolParam('Read', { file_path: '/src/index.ts' }) // '/src/index.ts'
13
+ * getRelevantToolParam('Bash', { command: 'npm test' }) // 'npm test'
14
+ * getRelevantToolParam('Grep', { pattern: 'TODO' }) // 'TODO'
15
+ */
16
+ export declare function getRelevantToolParam(toolName: string, input: Record<string, unknown> | undefined): string | undefined;
17
+ /**
18
+ * Format a tool invocation for display.
19
+ *
20
+ * @param toolName - The name of the tool being invoked
21
+ * @param input - The tool's input parameters
22
+ * @returns A formatted string like "**Read** /src/index.ts"
23
+ */
24
+ export declare function formatToolUse(toolName: string, input: Record<string, unknown> | undefined): string;
25
+ //# sourceMappingURL=tool-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-utils.d.ts","sourceRoot":"","sources":["../../src/shared/tool-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GACzC,MAAM,GAAG,SAAS,CAkBpB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GACzC,MAAM,CAQR"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Utility functions for extracting human-readable information from tool invocations.
3
+ */
4
+ /**
5
+ * Extract the most relevant parameter from a tool invocation for display.
6
+ *
7
+ * @param toolName - The name of the tool being invoked
8
+ * @param input - The tool's input parameters
9
+ * @returns The most relevant parameter value, or undefined
10
+ *
11
+ * @example
12
+ * getRelevantToolParam('Read', { file_path: '/src/index.ts' }) // '/src/index.ts'
13
+ * getRelevantToolParam('Bash', { command: 'npm test' }) // 'npm test'
14
+ * getRelevantToolParam('Grep', { pattern: 'TODO' }) // 'TODO'
15
+ */
16
+ export function getRelevantToolParam(toolName, input) {
17
+ if (!input)
18
+ return undefined;
19
+ switch (toolName) {
20
+ case 'Read':
21
+ case 'Write':
22
+ case 'Edit':
23
+ return input.file_path;
24
+ case 'Bash':
25
+ return input.command;
26
+ case 'Glob':
27
+ case 'Grep':
28
+ return input.pattern;
29
+ case 'LS':
30
+ return input.path;
31
+ default:
32
+ return undefined;
33
+ }
34
+ }
35
+ /**
36
+ * Format a tool invocation for display.
37
+ *
38
+ * @param toolName - The name of the tool being invoked
39
+ * @param input - The tool's input parameters
40
+ * @returns A formatted string like "**Read** /src/index.ts"
41
+ */
42
+ export function formatToolUse(toolName, input) {
43
+ const param = getRelevantToolParam(toolName, input);
44
+ if (param) {
45
+ return `**${toolName}** ${param}`;
46
+ }
47
+ return `**${toolName}**`;
48
+ }
49
+ //# sourceMappingURL=tool-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-utils.js","sourceRoot":"","sources":["../../src/shared/tool-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAClC,QAAgB,EAChB,KAA0C;IAE1C,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAE7B,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,MAAM,CAAC;QACZ,KAAK,OAAO,CAAC;QACb,KAAK,MAAM;YACT,OAAO,KAAK,CAAC,SAA+B,CAAC;QAC/C,KAAK,MAAM;YACT,OAAO,KAAK,CAAC,OAA6B,CAAC;QAC7C,KAAK,MAAM,CAAC;QACZ,KAAK,MAAM;YACT,OAAO,KAAK,CAAC,OAA6B,CAAC;QAC7C,KAAK,IAAI;YACP,OAAO,KAAK,CAAC,IAA0B,CAAC;QAC1C;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAC3B,QAAgB,EAChB,KAA0C;IAE1C,MAAM,KAAK,GAAG,oBAAoB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAEpD,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,KAAK,QAAQ,MAAM,KAAK,EAAE,CAAC;IACpC,CAAC;IAED,OAAO,KAAK,QAAQ,IAAI,CAAC;AAC3B,CAAC"}
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Core types shared between server and client
3
+ */
4
+ export interface Message {
5
+ id: string;
6
+ role: 'user' | 'assistant' | 'system';
7
+ content: string;
8
+ timestamp?: Date;
9
+ hidden?: boolean;
10
+ }
11
+ export type StreamSegment = {
12
+ type: 'text';
13
+ content: string;
14
+ } | {
15
+ type: 'tool';
16
+ name: string;
17
+ param?: string;
18
+ };
19
+ export type ClaudeError = {
20
+ code: 'rate_limit';
21
+ message: string;
22
+ retryAfter?: number;
23
+ } | {
24
+ code: 'auth_failed';
25
+ message: string;
26
+ } | {
27
+ code: 'credit_limit';
28
+ message: string;
29
+ } | {
30
+ code: 'process_crashed';
31
+ message: string;
32
+ exitCode: number;
33
+ } | {
34
+ code: 'network';
35
+ message: string;
36
+ } | {
37
+ code: 'unknown';
38
+ message: string;
39
+ };
40
+ export type SSEEvent = {
41
+ type: 'init';
42
+ sessionId: string;
43
+ resumed?: boolean;
44
+ prewarmed?: boolean;
45
+ } | {
46
+ type: 'history';
47
+ messages: Message[];
48
+ sessionId: string;
49
+ } | {
50
+ type: 'assistant';
51
+ text: string;
52
+ sessionId?: string;
53
+ } | {
54
+ type: 'tool';
55
+ name: string;
56
+ param?: string;
57
+ sessionId?: string;
58
+ } | {
59
+ type: 'result';
60
+ result: string;
61
+ sessionId?: string;
62
+ } | {
63
+ type: 'user';
64
+ content: string;
65
+ sessionId?: string;
66
+ } | {
67
+ type: 'error';
68
+ error: ClaudeError;
69
+ } | {
70
+ type: 'closed';
71
+ code?: number;
72
+ } | {
73
+ type: 'cleared';
74
+ } | {
75
+ type: 'stopped';
76
+ };
77
+ export interface ReconnectConfig {
78
+ maxRetries?: number;
79
+ baseDelay?: number;
80
+ maxDelay?: number;
81
+ }
82
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAC;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,IAAI,CAAC;IACjB,MAAM,C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IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GAClE;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,WAAW,CAAA;CAAE,GACrC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC;AAGxB,MAAM,WAAW,eAAe;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Core types shared between server and client
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@fronx/use-claude-code",
3
+ "version": "0.1.0",
4
+ "description": "Library for integrating Claude Code CLI into applications",
5
+ "type": "module",
6
+ "exports": {
7
+ "./server": {
8
+ "types": "./dist/server/index.d.ts",
9
+ "import": "./dist/server/index.js"
10
+ },
11
+ "./client": {
12
+ "types": "./dist/client/index.d.ts",
13
+ "import": "./dist/client/index.js"
14
+ },
15
+ "./react": {
16
+ "types": "./dist/react/index.d.ts",
17
+ "import": "./dist/react/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsc",
25
+ "dev": "tsc --watch"
26
+ },
27
+ "peerDependencies": {
28
+ "react": "^18.0.0 || ^19.0.0"
29
+ },
30
+ "peerDependenciesMeta": {
31
+ "react": {
32
+ "optional": true
33
+ }
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^20.0.0",
37
+ "@types/react": "^18.0.0",
38
+ "typescript": "^5.0.0"
39
+ }
40
+ }