@code-inspector/core 1.4.2 → 2.0.0-beta.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-inspector/core",
3
- "version": "1.4.2",
3
+ "version": "2.0.0-beta.1",
4
4
  "main": "dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "types/index.d.ts",
@@ -41,7 +41,8 @@
41
41
  "@vue/compiler-dom": "^3.5.13",
42
42
  "chalk": "^4.1.1",
43
43
  "dotenv": "^16.1.4",
44
- "launch-ide": "1.4.0",
44
+ "launch-ide": "1.4.2",
45
+ "marked": "^17.0.1",
45
46
  "portfinder": "^1.0.28"
46
47
  },
47
48
  "devDependencies": {
@@ -49,6 +50,7 @@
49
50
  "@babel/plugin-proposal-decorators": "^7.22.7",
50
51
  "@babel/plugin-syntax-import-meta": "^7.10.4",
51
52
  "@babel/plugin-transform-typescript": "^7.21.3",
53
+ "@floating-ui/dom": "^1.7.5",
52
54
  "@types/node": "^18.14.1",
53
55
  "@vue/babel-plugin-jsx": "^1.1.1",
54
56
  "@vue/compiler-sfc": "^3.3.4",
@@ -61,6 +63,9 @@
61
63
  "vite-plugin-node-stdlib-browser": "^0.2.1",
62
64
  "volar-service-pug": "^0.0.63"
63
65
  },
66
+ "optionalDependencies": {
67
+ "@anthropic-ai/claude-agent-sdk": "^0.2.29"
68
+ },
64
69
  "scripts": {
65
70
  "dev": "vite",
66
71
  "build:server": "vite build",
@@ -0,0 +1,112 @@
1
+ /**
2
+ * AI 模块 - 客户端 AI 聊天功能相关类型、模板和样式
3
+ */
4
+ import { TemplateResult } from 'lit';
5
+ /**
6
+ * 设置项目根路径(用于将绝对路径转为相对路径)
7
+ */
8
+ export declare function setProjectRoot(root: string): void;
9
+ /**
10
+ * 工具调用信息
11
+ */
12
+ export interface ToolCall {
13
+ id: string;
14
+ name: string;
15
+ input?: Record<string, any>;
16
+ result?: string;
17
+ isError?: boolean;
18
+ isComplete?: boolean;
19
+ }
20
+ /**
21
+ * 消息内容块
22
+ */
23
+ export interface ContentBlock {
24
+ type: 'text' | 'tool';
25
+ content?: string;
26
+ tool?: ToolCall;
27
+ }
28
+ /**
29
+ * 聊天消息类型
30
+ */
31
+ export interface ChatMessage {
32
+ role: 'user' | 'assistant';
33
+ content: string;
34
+ blocks?: ContentBlock[];
35
+ }
36
+ /**
37
+ * 聊天上下文信息(当前选中的元素)
38
+ */
39
+ export interface ChatContext {
40
+ file: string;
41
+ line: number;
42
+ column: number;
43
+ name: string;
44
+ }
45
+ /**
46
+ * 聊天状态接口
47
+ */
48
+ export interface ChatState {
49
+ showChatModal: boolean;
50
+ chatMessages: ChatMessage[];
51
+ chatInput: string;
52
+ chatLoading: boolean;
53
+ chatContext: ChatContext | null;
54
+ currentTools: Map<string, ToolCall>;
55
+ chatTheme: 'light' | 'dark';
56
+ turnStatus: 'idle' | 'running' | 'done' | 'interrupt';
57
+ turnDuration: number;
58
+ isDragging: boolean;
59
+ chatModel: string;
60
+ }
61
+ /**
62
+ * 聊天功能处理器接口
63
+ */
64
+ export interface ChatHandlers {
65
+ closeChatModal: () => void;
66
+ clearChatMessages: () => void;
67
+ handleChatInput: (e: Event) => void;
68
+ handleChatKeyDown: (e: KeyboardEvent) => void;
69
+ sendChatMessage: () => void;
70
+ toggleTheme: () => void;
71
+ interruptChat: () => void;
72
+ handleDragStart: (e: MouseEvent) => void;
73
+ handleDragMove: (e: MouseEvent) => void;
74
+ handleDragEnd: () => void;
75
+ handleOverlayClick: () => void;
76
+ }
77
+ /**
78
+ * 更新聊天框位置(使用 floating-ui)
79
+ * @param referenceEl 参考元素(选中的 DOM 元素)
80
+ * @param floatingEl 浮动元素(聊天框)
81
+ * @returns cleanup 函数
82
+ */
83
+ export declare function updateChatModalPosition(referenceEl: HTMLElement | null, floatingEl: HTMLElement | null): (() => void) | null;
84
+ /**
85
+ * 渲染聊天框模板
86
+ */
87
+ export declare function renderChatModal(state: ChatState, handlers: ChatHandlers): TemplateResult;
88
+ /**
89
+ * 聊天框样式 - 命令行风格
90
+ */
91
+ export declare const chatStyles: import("lit").CSSResult;
92
+ /**
93
+ * 流式事件处理器
94
+ */
95
+ export interface StreamHandlers {
96
+ onText: (content: string) => void;
97
+ onToolStart: (toolId: string, toolName: string, index: number) => void;
98
+ onToolInput: (index: number, input: Record<string, any>) => void;
99
+ onToolResult: (toolUseId: string, content: string, isError?: boolean) => void;
100
+ onError: (error: Error) => void;
101
+ onSessionId?: (sessionId: string) => void;
102
+ onProjectRoot?: (cwd: string) => void;
103
+ onModel?: (model: string) => void;
104
+ }
105
+ /**
106
+ * 获取 AI 模型信息
107
+ */
108
+ export declare function fetchModelInfo(ip: string, port: number): Promise<string>;
109
+ /**
110
+ * 发送聊天消息到服务器
111
+ */
112
+ export declare function sendChatToServer(ip: string, port: number, message: string, context: ChatContext | null, history: ChatMessage[], handlers: StreamHandlers, signal?: AbortSignal, sessionId?: string | null): Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import { LitElement, TemplateResult } from 'lit';
2
+ import { ChatMessage, ChatContext, ToolCall } from './ai';
2
3
  interface Position {
3
4
  left?: string;
4
5
  right?: string;
@@ -42,12 +43,14 @@ export declare class CodeInspectorComponent extends LitElement {
42
43
  autoToggle: boolean;
43
44
  hideConsole: boolean;
44
45
  locate: boolean;
45
- copy: boolean | string;
46
+ copy: boolean | undefined | string;
46
47
  target: string;
47
48
  targetNode: HTMLElement | null;
48
49
  ip: string;
50
+ ai: boolean;
49
51
  private wheelThrottling;
50
52
  modeKey: string;
53
+ defaultAction: string;
51
54
  position: {
52
55
  top: number;
53
56
  right: number;
@@ -101,6 +104,28 @@ export declare class CodeInspectorComponent extends LitElement {
101
104
  internalLocate: boolean;
102
105
  internalCopy: boolean;
103
106
  internalTarget: boolean;
107
+ internalAI: boolean;
108
+ showChatModal: boolean;
109
+ chatMessages: ChatMessage[];
110
+ chatInput: string;
111
+ chatLoading: boolean;
112
+ chatContext: ChatContext | null;
113
+ currentTools: Map<string, ToolCall>;
114
+ chatSessionId: string | null;
115
+ chatTheme: 'light' | 'dark';
116
+ turnStatus: 'idle' | 'running' | 'done' | 'interrupt';
117
+ turnDuration: number;
118
+ chatModel: string;
119
+ private chatAbortController;
120
+ private turnTimerInterval;
121
+ private turnStartTime;
122
+ isDragging: boolean;
123
+ private dragStartX;
124
+ private dragStartY;
125
+ private modalStartX;
126
+ private modalStartY;
127
+ private wasDragging;
128
+ private chatPositionCleanup;
104
129
  inspectorSwitchRef: HTMLDivElement;
105
130
  codeInspectorContainerRef: HTMLDivElement;
106
131
  elementInfoRef: HTMLDivElement;
@@ -112,6 +137,10 @@ export declare class CodeInspectorComponent extends LitElement {
112
137
  description: string;
113
138
  checked: () => boolean;
114
139
  onChange: () => void;
140
+ action: string;
141
+ fn: () => void;
142
+ key: number;
143
+ available: () => boolean;
115
144
  }[];
116
145
  private eventListeners;
117
146
  isTracking: (e: any) => boolean | "";
@@ -147,6 +176,10 @@ export declare class CodeInspectorComponent extends LitElement {
147
176
  sendXHR: () => void;
148
177
  sendImg: () => void;
149
178
  buildTargetUrl: () => string;
179
+ locateCode: () => void;
180
+ copyCode: () => void;
181
+ targetCode: () => void;
182
+ dispatchCustomEvent: (action: 'locate' | 'copy' | 'target' | 'chat' | string) => void;
150
183
  trackCode: () => void;
151
184
  private handleModeShortcut;
152
185
  showNotification(message: string, type?: 'success' | 'error'): void;
@@ -178,9 +211,27 @@ export declare class CodeInspectorComponent extends LitElement {
178
211
  handleMouseLeaveNode: () => void;
179
212
  toggleSettingsModal: () => void;
180
213
  closeSettingsModal: () => void;
214
+ private clearAllActions;
181
215
  toggleLocate: () => void;
182
216
  toggleCopy: () => void;
183
217
  toggleTarget: () => void;
218
+ toggleAICode: () => void;
219
+ openChatModal: () => void;
220
+ closeChatModal: () => void;
221
+ clearChatMessages: () => void;
222
+ toggleTheme: () => void;
223
+ handleChatInput: (e: Event) => void;
224
+ handleChatKeyDown: (e: KeyboardEvent) => void;
225
+ private scrollPending;
226
+ private scrollChatToBottom;
227
+ private startTurnTimer;
228
+ private stopTurnTimer;
229
+ interruptChat: () => void;
230
+ handleChatDragStart: (e: MouseEvent) => void;
231
+ handleChatDragMove: (e: MouseEvent) => void;
232
+ handleChatDragEnd: () => void;
233
+ handleOverlayClick: () => void;
234
+ sendChatMessage: () => Promise<void>;
184
235
  /**
185
236
  * Attach all event listeners
186
237
  */
@@ -193,6 +244,6 @@ export declare class CodeInspectorComponent extends LitElement {
193
244
  disconnectedCallback(): void;
194
245
  renderNodeTree: (node: TreeNode) => TemplateResult;
195
246
  render(): TemplateResult<1>;
196
- static styles: import("lit").CSSResult;
247
+ static styles: import("lit").CSSResult[];
197
248
  }
198
249
  export {};
@@ -0,0 +1,19 @@
1
+ import type { AIOptions } from '../shared';
2
+ import type { AIContext, AIMessage } from './ai';
3
+ export interface ProviderCallbacks {
4
+ sendSSE: (data: object | string) => void;
5
+ onEnd: () => void;
6
+ }
7
+ export interface ProviderResult {
8
+ abort: () => void;
9
+ }
10
+ /**
11
+ * 获取模型信息
12
+ * 优先使用用户配置,否则通过 CLI 的 system 事件获取(无 API 消耗)
13
+ */
14
+ export declare function getModelInfo(aiOptions: AIOptions | undefined): Promise<string>;
15
+ /**
16
+ * Claude provider 统一入口
17
+ * ai.ts 只需调用此函数,不感知 CLI/SDK 细节
18
+ */
19
+ export declare function handleClaudeRequest(message: string, context: AIContext | null, history: AIMessage[], sessionId: string | undefined, cwd: string, aiOptions: AIOptions | undefined, callbacks: ProviderCallbacks): ProviderResult;
@@ -0,0 +1,48 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * AI 功能模块 - 处理与 AI Agent 的交互
4
+ * 通过 provider 模式支持不同的 AI 后端
5
+ */
6
+ import http from 'http';
7
+ import type { AIOptions } from '../shared';
8
+ /**
9
+ * AI 上下文信息
10
+ */
11
+ export interface AIContext {
12
+ file: string;
13
+ line: number;
14
+ column: number;
15
+ name: string;
16
+ }
17
+ /**
18
+ * AI 消息
19
+ */
20
+ export interface AIMessage {
21
+ role: 'user' | 'assistant';
22
+ content: string;
23
+ }
24
+ /**
25
+ * AI 请求体
26
+ */
27
+ export interface AIRequest {
28
+ message: string;
29
+ context: AIContext;
30
+ history: AIMessage[];
31
+ sessionId?: string;
32
+ }
33
+ /**
34
+ * 从 behavior 配置中提取 AI 选项
35
+ */
36
+ export declare function getAIOptions(behavior?: {
37
+ ai?: {
38
+ claudeCode?: boolean | AIOptions;
39
+ };
40
+ }): AIOptions | undefined;
41
+ /**
42
+ * 处理 AI 请求
43
+ */
44
+ export declare function handleAIRequest(req: http.IncomingMessage, res: http.ServerResponse, corsHeaders: Record<string, string>, aiOptions: AIOptions | undefined, projectRootPath: string): Promise<void>;
45
+ /**
46
+ * 处理 AI 模型信息请求
47
+ */
48
+ export declare function handleAIModelRequest(res: http.ServerResponse, corsHeaders: Record<string, string>, aiOptions: AIOptions | undefined): Promise<void>;
@@ -1,8 +1,25 @@
1
1
  /// <reference types="node" />
2
+ /**
3
+ * 本地服务器模块 - 处理 IDE 打开和 AI 请求
4
+ */
2
5
  import http from 'http';
3
- import type { PathType, CodeOptions, RecordInfo } from '../shared';
6
+ import type { CodeOptions, RecordInfo } from '../shared';
7
+ export declare function getEnvVars(): Record<string, string>;
8
+ /** 项目根目录 */
4
9
  export declare const ProjectRootPath: string;
10
+ /**
11
+ * 获取相对路径
12
+ */
5
13
  export declare function getRelativePath(filePath: string): string;
6
- export declare function getRelativeOrAbsolutePath(filePath: string, pathType?: PathType): string;
7
- export declare function createServer(callback: (port: number) => any, options?: CodeOptions, record?: RecordInfo): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
14
+ /**
15
+ * 根据用户配置返回绝对路径或者相对路径
16
+ */
17
+ export declare function getRelativeOrAbsolutePath(filePath: string, pathType?: 'relative' | 'absolute'): string;
18
+ /**
19
+ * 创建 HTTP 服务器
20
+ */
21
+ export declare function createServer(callback: (port: number) => void, options?: CodeOptions, record?: RecordInfo): http.Server;
22
+ /**
23
+ * 启动服务器
24
+ */
8
25
  export declare function startServer(options: CodeOptions, record: RecordInfo): Promise<void>;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * 浏览器环境 console 彩色输出工具
3
+ *
4
+ * 使用方式类似 chalk,基于浏览器 console 的 %c 机制实现:
5
+ * @example
6
+ * ```ts
7
+ * // 单行输出
8
+ * browserChalk.blue('[plugin]').green(' ready').log();
9
+ *
10
+ * // 多行输出
11
+ * browserChalk
12
+ * .blue('[plugin]')
13
+ * .line()
14
+ * .green('• line1')
15
+ * .line()
16
+ * .yellow('• line2')
17
+ * .log();
18
+ * ```
19
+ */
20
+ interface ChainResult {
21
+ text: string;
22
+ styles: string[];
23
+ }
24
+ export declare class BrowserChalkChain {
25
+ private parts;
26
+ push(text: string, style: string): this;
27
+ /** 换行 */
28
+ line(): this;
29
+ /** 合并另一个 chain 的内容 */
30
+ merge(other: BrowserChalkChain): this;
31
+ /** 构建最终的 text 和 styles 数组 */
32
+ build(): ChainResult;
33
+ /** 直接 console.log 输出 */
34
+ log(): void;
35
+ /** 直接 console.warn 输出 */
36
+ warn(): void;
37
+ /** 直接 console.error 输出 */
38
+ error(): void;
39
+ /** console.groupCollapsed 输出,label 为当前 chain 内容,fn 中输出 group 内容 */
40
+ groupCollapsed(fn: () => void): void;
41
+ group(fn: () => void): void;
42
+ blue(t: string): this;
43
+ green(t: string): this;
44
+ yellow(t: string): this;
45
+ red(t: string): this;
46
+ gray(t: string): this;
47
+ text(t: string): this;
48
+ styled(t: string, css: string): this;
49
+ /** 无参数:给上一个 part 追加 bold;有参数:输出纯 bold 文本 */
50
+ bold(t?: string): this;
51
+ style(style: string): this;
52
+ }
53
+ type ChalkFactory = {
54
+ [K in 'blue' | 'green' | 'yellow' | 'red' | 'gray' | 'text']: (t: string) => BrowserChalkChain;
55
+ } & {
56
+ bold: (t?: string) => BrowserChalkChain;
57
+ styled: (t: string, css: string) => BrowserChalkChain;
58
+ };
59
+ export declare const browserChalk: ChalkFactory;
60
+ export {};
@@ -2,11 +2,57 @@
2
2
  import { Server } from 'http';
3
3
  import type { Editor } from 'launch-ide';
4
4
  export type HotKey = 'ctrlKey' | 'altKey' | 'metaKey' | 'shiftKey';
5
+ export type AIOptions = {
6
+ /**
7
+ * @zh 指定使用的 Agent 类型。'cli' 使用本地 Claude Code CLI,'sdk' 使用 Claude Agent SDK。默认为 'cli'
8
+ * @en Specify the agent type to use. 'cli' uses local Claude Code CLI, 'sdk' uses Claude Agent SDK. Defaults to 'cli'
9
+ */
10
+ agent?: 'cli' | 'sdk';
11
+ /**
12
+ * @zh SDK 选项,参数格式继承 @anthropic-ai/claude-agent-sdk 官方 SDK 的 Options 类型
13
+ * @en SDK options, parameter format follows the official @anthropic-ai/claude-agent-sdk Options type
14
+ * @see https://www.npmjs.com/package/@anthropic-ai/claude-agent-sdk
15
+ */
16
+ sdkOptions?: {
17
+ /** 允许自动执行的工具列表 */
18
+ allowedTools?: string[];
19
+ /** 禁止的工具列表 */
20
+ disallowedTools?: string[];
21
+ /** 使用的模型 */
22
+ model?: string;
23
+ /** 最大执行轮数,默认为 20 */
24
+ maxTurns?: number;
25
+ /**
26
+ * 权限模式。默认为 'bypassPermissions'
27
+ * - 'default' 需要用户确认
28
+ * - 'acceptEdits' 自动接受编辑
29
+ * - 'bypassPermissions' 绕过所有权限检查
30
+ */
31
+ permissionMode?: 'default' | 'acceptEdits' | 'bypassPermissions';
32
+ /** 系统提示 */
33
+ systemPrompt?: string | {
34
+ type: 'preset';
35
+ preset: 'claude_code';
36
+ append?: string;
37
+ };
38
+ /** 环境变量,传递给 Claude Code 进程。默认为 process.env */
39
+ env?: Record<string, string | undefined>;
40
+ /** MCP 服务器配置 */
41
+ mcpServers?: Record<string, any>;
42
+ /** 最大思考 token 数 */
43
+ maxThinkingTokens?: number;
44
+ /** 最大预算(美元) */
45
+ maxBudgetUsd?: number;
46
+ };
47
+ };
5
48
  export type Behavior = {
6
49
  locate?: boolean;
7
50
  copy?: boolean | string;
8
51
  target?: string;
9
- defaultAction?: 'copy' | 'locate' | 'target' | 'all';
52
+ ai?: {
53
+ claudeCode?: boolean | AIOptions;
54
+ };
55
+ defaultAction?: 'copy' | 'locate' | 'target' | 'ai';
10
56
  };
11
57
  export type RecordInfo = {
12
58
  port: number;