chatccc 0.2.184 → 0.2.185
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/README.md +88 -69
- package/config.sample.json +5 -0
- package/im-skills/feishu-skill/skill.md +3 -3
- package/package.json +1 -1
- package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -0
- package/src/__tests__/chatgpt-subscription.test.ts +135 -0
- package/src/__tests__/chrome-devtools-guard.test.ts +125 -0
- package/src/__tests__/config-reload.test.ts +13 -0
- package/src/__tests__/config-sample.test.ts +11 -0
- package/src/__tests__/orchestrator.test.ts +162 -117
- package/src/__tests__/web-ui.test.ts +16 -0
- package/src/agent-delegate-task-rpc.ts +153 -153
- package/src/agent-delegate-task.ts +81 -81
- package/src/chatgpt-subscription-rpc.ts +27 -0
- package/src/chatgpt-subscription.ts +299 -0
- package/src/chrome-devtools-guard.ts +242 -0
- package/src/config.ts +21 -0
- package/src/index.ts +12 -4
- package/src/orchestrator.ts +41 -4
- package/src/session-name.ts +8 -8
- package/src/session.ts +6 -6
- package/src/web-ui.ts +136 -6
|
@@ -33,6 +33,22 @@ describe("unflattenConfig", () => {
|
|
|
33
33
|
},
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
|
+
|
|
37
|
+
it("maps Chrome CDP guard fields into chromeDevtools config", () => {
|
|
38
|
+
expect(
|
|
39
|
+
unflattenConfig({
|
|
40
|
+
CHATCCC_CHROME_DEVTOOLS_ENABLED: true,
|
|
41
|
+
CHATCCC_CHROME_DEVTOOLS_PORT: "15166",
|
|
42
|
+
CHATCCC_CHROME_DEVTOOLS_PATH: "C:/Program Files/Google/Chrome/Application/chrome.exe",
|
|
43
|
+
}),
|
|
44
|
+
).toEqual({
|
|
45
|
+
chromeDevtools: {
|
|
46
|
+
enabled: true,
|
|
47
|
+
port: 15166,
|
|
48
|
+
chromePath: "C:/Program Files/Google/Chrome/Application/chrome.exe",
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
36
52
|
});
|
|
37
53
|
|
|
38
54
|
describe("dashboard edit modal", () => {
|
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import { resolve } from "node:path";
|
|
3
|
-
|
|
4
|
-
import { resolveDefaultAgentTool } from "./config.ts";
|
|
5
|
-
import { readUtf8JsonBody } from "./agent-rpc-body.ts";
|
|
6
|
-
import { delegateAgentTask } from "./agent-delegate-task.ts";
|
|
7
|
-
import type { PlatformAdapter } from "./platform-adapter.ts";
|
|
8
|
-
import { applySharedPrefix } from "./shared-prefix.ts";
|
|
9
|
-
|
|
10
|
-
export const AGENT_DELEGATE_TASK_PATH = "/api/agent/delegate-task";
|
|
11
|
-
|
|
12
|
-
const MAX_REQUEST_BYTES = 128 * 1024;
|
|
13
|
-
const VALID_TOOLS = new Set(["claude", "cursor", "codex"]);
|
|
14
|
-
|
|
15
|
-
interface AgentDelegateTaskPayload {
|
|
16
|
-
tool?: unknown;
|
|
17
|
-
cwd?: unknown;
|
|
18
|
-
prompt?: unknown;
|
|
19
|
-
text?: unknown;
|
|
20
|
-
message?: unknown;
|
|
21
|
-
open_id?: unknown;
|
|
22
|
-
open_ids?: unknown;
|
|
23
|
-
openIds?: unknown;
|
|
24
|
-
chat_name?: unknown;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function jsonReply(res: ServerResponse, status: number, data: unknown): void {
|
|
28
|
-
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
29
|
-
res.end(JSON.stringify(data));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function stringValue(value: unknown): string {
|
|
33
|
-
return typeof value === "string" ? value.trim() : "";
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function normalizeOpenIds(payload: AgentDelegateTaskPayload): string[] {
|
|
37
|
-
const explicitOpenId = stringValue(payload.open_id);
|
|
38
|
-
if (explicitOpenId) return [explicitOpenId];
|
|
39
|
-
|
|
40
|
-
const rawOpenIds = Array.isArray(payload.open_ids) ? payload.open_ids : payload.openIds;
|
|
41
|
-
if (!Array.isArray(rawOpenIds)) return [];
|
|
42
|
-
return rawOpenIds
|
|
43
|
-
.filter((item): item is string => typeof item === "string")
|
|
44
|
-
.map((item) => item.trim())
|
|
45
|
-
.filter(Boolean);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function promptFromPayload(payload: AgentDelegateTaskPayload): string {
|
|
49
|
-
return stringValue(payload.prompt) || stringValue(payload.text) || stringValue(payload.message);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function validateTool(rawTool: unknown): string {
|
|
53
|
-
const tool = stringValue(rawTool).toLowerCase() || resolveDefaultAgentTool();
|
|
54
|
-
if (!VALID_TOOLS.has(tool)) throw new Error(`unsupported tool: ${tool}`);
|
|
55
|
-
return tool;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function validateCwd(rawCwd: unknown): string {
|
|
59
|
-
const cwd = stringValue(rawCwd);
|
|
60
|
-
if (!cwd) throw new Error("cwd must be a non-empty string");
|
|
61
|
-
return resolve(cwd);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export async function handleAgentDelegateTaskRequest(
|
|
65
|
-
req: IncomingMessage,
|
|
66
|
-
res: ServerResponse,
|
|
67
|
-
platform: PlatformAdapter,
|
|
68
|
-
): Promise<boolean> {
|
|
69
|
-
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
70
|
-
if (url.pathname !== AGENT_DELEGATE_TASK_PATH) return false;
|
|
71
|
-
|
|
72
|
-
if (req.method !== "POST") {
|
|
73
|
-
jsonReply(res, 405, { ok: false, error: "Method not allowed" });
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (platform.kind !== "feishu") {
|
|
78
|
-
jsonReply(res, 409, { ok: false, error: "This endpoint currently only supports Feishu." });
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
let payload: AgentDelegateTaskPayload;
|
|
83
|
-
try {
|
|
84
|
-
payload = await readUtf8JsonBody(req, MAX_REQUEST_BYTES);
|
|
85
|
-
} catch (err) {
|
|
86
|
-
jsonReply(res, 400, { ok: false, error: (err as Error).message || "Invalid JSON" });
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
let tool: string;
|
|
91
|
-
let cwd: string;
|
|
92
|
-
let promptText: string;
|
|
93
|
-
let promptNamePrefix: string;
|
|
94
|
-
let openIds: string[];
|
|
95
|
-
try {
|
|
96
|
-
tool = validateTool(payload.tool);
|
|
97
|
-
cwd = validateCwd(payload.cwd);
|
|
98
|
-
const rawPrompt = promptFromPayload(payload);
|
|
99
|
-
if (!rawPrompt) throw new Error("prompt must be a non-empty string");
|
|
100
|
-
const sharedPrefix = applySharedPrefix(rawPrompt);
|
|
101
|
-
promptText = sharedPrefix.text;
|
|
102
|
-
promptNamePrefix = sharedPrefix.body || rawPrompt;
|
|
103
|
-
openIds = normalizeOpenIds(payload);
|
|
104
|
-
if (openIds.length === 0) throw new Error("open_id or openIds must include at least one user");
|
|
105
|
-
} catch (err) {
|
|
106
|
-
jsonReply(res, 400, { ok: false, error: (err as Error).message });
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
const result = await delegateAgentTask({
|
|
112
|
-
platform,
|
|
113
|
-
tool,
|
|
114
|
-
cwd,
|
|
115
|
-
promptText,
|
|
116
|
-
openIds,
|
|
117
|
-
chatNamePrefix: stringValue(payload.chat_name) || promptNamePrefix.slice(0, 10),
|
|
118
|
-
});
|
|
119
|
-
jsonReply(res, 200, {
|
|
120
|
-
ok: true,
|
|
121
|
-
chat_id: result.chatId,
|
|
122
|
-
session_id: result.sessionId,
|
|
123
|
-
tool: result.tool,
|
|
124
|
-
cwd: result.cwd,
|
|
125
|
-
});
|
|
126
|
-
} catch (err) {
|
|
127
|
-
jsonReply(res, 500, { ok: false, error: (err as Error).message });
|
|
128
|
-
}
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function buildAgentDelegateTaskCapabilityPrompt(input: { url: string; cwd?: string }): string {
|
|
133
|
-
const lines = [
|
|
134
|
-
"[ChatCCC local capability: delegate task]",
|
|
135
|
-
"You can create a separate Feishu ChatCCC agent session and assign its first task by calling this local endpoint.",
|
|
136
|
-
"",
|
|
137
|
-
`POST ${input.url}`,
|
|
138
|
-
"Content-Type: application/json; charset=utf-8",
|
|
139
|
-
"",
|
|
140
|
-
'Body: {"tool":"codex|claude|cursor","cwd":"absolute working directory","open_id":"Feishu open_id to invite","prompt":"first task text"}',
|
|
141
|
-
"",
|
|
142
|
-
"Rules:",
|
|
143
|
-
"- Use this only when the user asks you to start a separate delegated conversation/session.",
|
|
144
|
-
"- Pass cwd explicitly as an absolute local path.",
|
|
145
|
-
"- Pass tool explicitly when the user specified a target agent.",
|
|
146
|
-
"- Use open_id for one user or open_ids/openIds for multiple users.",
|
|
147
|
-
"- The prompt is sent through the normal ChatCCC prompt path, so project prompt injection and IM skills still apply.",
|
|
148
|
-
"- Request body must be UTF-8 encoded JSON bytes. Do not call Feishu Open Platform directly.",
|
|
149
|
-
"[/ChatCCC local capability: delegate task]",
|
|
150
|
-
];
|
|
151
|
-
if (input.cwd) lines.splice(2, 0, `Current working directory: ${input.cwd}`);
|
|
152
|
-
return lines.join("\n");
|
|
153
|
-
}
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
import { resolveDefaultAgentTool } from "./config.ts";
|
|
5
|
+
import { readUtf8JsonBody } from "./agent-rpc-body.ts";
|
|
6
|
+
import { delegateAgentTask } from "./agent-delegate-task.ts";
|
|
7
|
+
import type { PlatformAdapter } from "./platform-adapter.ts";
|
|
8
|
+
import { applySharedPrefix } from "./shared-prefix.ts";
|
|
9
|
+
|
|
10
|
+
export const AGENT_DELEGATE_TASK_PATH = "/api/agent/delegate-task";
|
|
11
|
+
|
|
12
|
+
const MAX_REQUEST_BYTES = 128 * 1024;
|
|
13
|
+
const VALID_TOOLS = new Set(["claude", "cursor", "codex"]);
|
|
14
|
+
|
|
15
|
+
interface AgentDelegateTaskPayload {
|
|
16
|
+
tool?: unknown;
|
|
17
|
+
cwd?: unknown;
|
|
18
|
+
prompt?: unknown;
|
|
19
|
+
text?: unknown;
|
|
20
|
+
message?: unknown;
|
|
21
|
+
open_id?: unknown;
|
|
22
|
+
open_ids?: unknown;
|
|
23
|
+
openIds?: unknown;
|
|
24
|
+
chat_name?: unknown;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function jsonReply(res: ServerResponse, status: number, data: unknown): void {
|
|
28
|
+
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
29
|
+
res.end(JSON.stringify(data));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function stringValue(value: unknown): string {
|
|
33
|
+
return typeof value === "string" ? value.trim() : "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function normalizeOpenIds(payload: AgentDelegateTaskPayload): string[] {
|
|
37
|
+
const explicitOpenId = stringValue(payload.open_id);
|
|
38
|
+
if (explicitOpenId) return [explicitOpenId];
|
|
39
|
+
|
|
40
|
+
const rawOpenIds = Array.isArray(payload.open_ids) ? payload.open_ids : payload.openIds;
|
|
41
|
+
if (!Array.isArray(rawOpenIds)) return [];
|
|
42
|
+
return rawOpenIds
|
|
43
|
+
.filter((item): item is string => typeof item === "string")
|
|
44
|
+
.map((item) => item.trim())
|
|
45
|
+
.filter(Boolean);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function promptFromPayload(payload: AgentDelegateTaskPayload): string {
|
|
49
|
+
return stringValue(payload.prompt) || stringValue(payload.text) || stringValue(payload.message);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function validateTool(rawTool: unknown): string {
|
|
53
|
+
const tool = stringValue(rawTool).toLowerCase() || resolveDefaultAgentTool();
|
|
54
|
+
if (!VALID_TOOLS.has(tool)) throw new Error(`unsupported tool: ${tool}`);
|
|
55
|
+
return tool;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function validateCwd(rawCwd: unknown): string {
|
|
59
|
+
const cwd = stringValue(rawCwd);
|
|
60
|
+
if (!cwd) throw new Error("cwd must be a non-empty string");
|
|
61
|
+
return resolve(cwd);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function handleAgentDelegateTaskRequest(
|
|
65
|
+
req: IncomingMessage,
|
|
66
|
+
res: ServerResponse,
|
|
67
|
+
platform: PlatformAdapter,
|
|
68
|
+
): Promise<boolean> {
|
|
69
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
70
|
+
if (url.pathname !== AGENT_DELEGATE_TASK_PATH) return false;
|
|
71
|
+
|
|
72
|
+
if (req.method !== "POST") {
|
|
73
|
+
jsonReply(res, 405, { ok: false, error: "Method not allowed" });
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (platform.kind !== "feishu") {
|
|
78
|
+
jsonReply(res, 409, { ok: false, error: "This endpoint currently only supports Feishu." });
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let payload: AgentDelegateTaskPayload;
|
|
83
|
+
try {
|
|
84
|
+
payload = await readUtf8JsonBody(req, MAX_REQUEST_BYTES);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
jsonReply(res, 400, { ok: false, error: (err as Error).message || "Invalid JSON" });
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let tool: string;
|
|
91
|
+
let cwd: string;
|
|
92
|
+
let promptText: string;
|
|
93
|
+
let promptNamePrefix: string;
|
|
94
|
+
let openIds: string[];
|
|
95
|
+
try {
|
|
96
|
+
tool = validateTool(payload.tool);
|
|
97
|
+
cwd = validateCwd(payload.cwd);
|
|
98
|
+
const rawPrompt = promptFromPayload(payload);
|
|
99
|
+
if (!rawPrompt) throw new Error("prompt must be a non-empty string");
|
|
100
|
+
const sharedPrefix = applySharedPrefix(rawPrompt);
|
|
101
|
+
promptText = sharedPrefix.text;
|
|
102
|
+
promptNamePrefix = sharedPrefix.body || rawPrompt;
|
|
103
|
+
openIds = normalizeOpenIds(payload);
|
|
104
|
+
if (openIds.length === 0) throw new Error("open_id or openIds must include at least one user");
|
|
105
|
+
} catch (err) {
|
|
106
|
+
jsonReply(res, 400, { ok: false, error: (err as Error).message });
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
const result = await delegateAgentTask({
|
|
112
|
+
platform,
|
|
113
|
+
tool,
|
|
114
|
+
cwd,
|
|
115
|
+
promptText,
|
|
116
|
+
openIds,
|
|
117
|
+
chatNamePrefix: stringValue(payload.chat_name) || promptNamePrefix.slice(0, 10),
|
|
118
|
+
});
|
|
119
|
+
jsonReply(res, 200, {
|
|
120
|
+
ok: true,
|
|
121
|
+
chat_id: result.chatId,
|
|
122
|
+
session_id: result.sessionId,
|
|
123
|
+
tool: result.tool,
|
|
124
|
+
cwd: result.cwd,
|
|
125
|
+
});
|
|
126
|
+
} catch (err) {
|
|
127
|
+
jsonReply(res, 500, { ok: false, error: (err as Error).message });
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function buildAgentDelegateTaskCapabilityPrompt(input: { url: string; cwd?: string }): string {
|
|
133
|
+
const lines = [
|
|
134
|
+
"[ChatCCC local capability: delegate task]",
|
|
135
|
+
"You can create a separate Feishu ChatCCC agent session and assign its first task by calling this local endpoint.",
|
|
136
|
+
"",
|
|
137
|
+
`POST ${input.url}`,
|
|
138
|
+
"Content-Type: application/json; charset=utf-8",
|
|
139
|
+
"",
|
|
140
|
+
'Body: {"tool":"codex|claude|cursor","cwd":"absolute working directory","open_id":"Feishu open_id to invite","prompt":"first task text"}',
|
|
141
|
+
"",
|
|
142
|
+
"Rules:",
|
|
143
|
+
"- Use this only when the user asks you to start a separate delegated conversation/session.",
|
|
144
|
+
"- Pass cwd explicitly as an absolute local path.",
|
|
145
|
+
"- Pass tool explicitly when the user specified a target agent.",
|
|
146
|
+
"- Use open_id for one user or open_ids/openIds for multiple users.",
|
|
147
|
+
"- The prompt is sent through the normal ChatCCC prompt path, so project prompt injection and IM skills still apply.",
|
|
148
|
+
"- Request body must be UTF-8 encoded JSON bytes. Do not call Feishu Open Platform directly.",
|
|
149
|
+
"[/ChatCCC local capability: delegate task]",
|
|
150
|
+
];
|
|
151
|
+
if (input.cwd) lines.splice(2, 0, `Current working directory: ${input.cwd}`);
|
|
152
|
+
return lines.join("\n");
|
|
153
|
+
}
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
2
|
-
|
|
3
|
-
import { sessionPrefixForTool, toolDisplayName, ts } from "./config.ts";
|
|
4
|
-
import { setDefaultCwd } from "./config.ts";
|
|
5
|
-
import type { PlatformAdapter } from "./platform-adapter.ts";
|
|
6
|
-
import { initClaudeSession, recordSessionRegistry, resumeAndPrompt, saveSessionTool } from "./session.ts";
|
|
7
|
-
import { bindChatToSession } from "./session-chat-binding.ts";
|
|
8
|
-
import { sessionChatName } from "./session-name.ts";
|
|
9
|
-
|
|
10
|
-
export interface DelegateAgentTaskInput {
|
|
11
|
-
platform: PlatformAdapter;
|
|
12
|
-
tool: string;
|
|
13
|
-
cwd: string;
|
|
14
|
-
promptText: string;
|
|
15
|
-
openIds: string[];
|
|
16
|
-
chatNamePrefix?: string;
|
|
17
|
-
msgTimestamp?: number;
|
|
18
|
-
traceId?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface DelegateAgentTaskResult {
|
|
22
|
-
chatId: string;
|
|
23
|
-
sessionId: string;
|
|
24
|
-
tool: string;
|
|
25
|
-
cwd: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function delegateAgentTask(input: DelegateAgentTaskInput): Promise<DelegateAgentTaskResult> {
|
|
29
|
-
const cwd = resolve(input.cwd);
|
|
30
|
-
const toolLabel = toolDisplayName(input.tool);
|
|
31
|
-
const init = await initClaudeSession(input.tool, cwd);
|
|
32
|
-
const sessionId = init.sessionId;
|
|
33
|
-
const chatNamePrefix = input.chatNamePrefix?.trim() || input.promptText.slice(0, 10) || "新会话";
|
|
34
|
-
const chatName = sessionChatName(chatNamePrefix, cwd);
|
|
35
|
-
|
|
36
|
-
let chatId: string;
|
|
37
|
-
try {
|
|
38
|
-
chatId = await input.platform.createGroup(chatName, input.openIds);
|
|
39
|
-
await input.platform.updateChatInfo(chatId, chatName, `${sessionPrefixForTool(input.tool)} ${sessionId}`);
|
|
40
|
-
await setDefaultCwd(cwd, chatId);
|
|
41
|
-
bindChatToSession(sessionId, chatId);
|
|
42
|
-
await recordSessionRegistry({
|
|
43
|
-
chatId,
|
|
44
|
-
sessionId,
|
|
45
|
-
tool: input.tool,
|
|
46
|
-
chatName,
|
|
47
|
-
turnCount: 0,
|
|
48
|
-
startTime: Date.now(),
|
|
49
|
-
running: false,
|
|
50
|
-
});
|
|
51
|
-
await saveSessionTool(sessionId, input.tool, chatName);
|
|
52
|
-
} catch (err) {
|
|
53
|
-
console.error(`[${ts()}] [AGENT-DELEGATE-TASK] create group failed: ${(err as Error).message}`);
|
|
54
|
-
throw err;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
await input.platform.sendCard(
|
|
58
|
-
chatId,
|
|
59
|
-
`${toolLabel} Session Ready`,
|
|
60
|
-
`已创建 **${toolLabel}** 会话群。\n\n` +
|
|
61
|
-
`**Session ID:** ${sessionId}\n` +
|
|
62
|
-
`**工作目录:** \`${cwd}\`\n\n` +
|
|
63
|
-
`下面会自动把任务作为第一句话发送给 ${toolLabel}。`,
|
|
64
|
-
"green",
|
|
65
|
-
).catch(() => {});
|
|
66
|
-
input.platform.setChatAvatar(chatId, input.tool, "new").catch(() => {});
|
|
67
|
-
|
|
68
|
-
await resumeAndPrompt(
|
|
69
|
-
sessionId,
|
|
70
|
-
input.promptText,
|
|
71
|
-
input.platform,
|
|
72
|
-
chatId,
|
|
73
|
-
input.msgTimestamp ?? Date.now(),
|
|
74
|
-
input.tool,
|
|
75
|
-
input.traceId,
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
console.log(`[${ts()}] [AGENT-DELEGATE-TASK] created ${toolLabel} session=${sessionId} chat=${chatId} cwd=${cwd}`);
|
|
79
|
-
return { chatId, sessionId, tool: input.tool, cwd };
|
|
80
|
-
}
|
|
81
|
-
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
|
|
3
|
+
import { sessionPrefixForTool, toolDisplayName, ts } from "./config.ts";
|
|
4
|
+
import { setDefaultCwd } from "./config.ts";
|
|
5
|
+
import type { PlatformAdapter } from "./platform-adapter.ts";
|
|
6
|
+
import { initClaudeSession, recordSessionRegistry, resumeAndPrompt, saveSessionTool } from "./session.ts";
|
|
7
|
+
import { bindChatToSession } from "./session-chat-binding.ts";
|
|
8
|
+
import { sessionChatName } from "./session-name.ts";
|
|
9
|
+
|
|
10
|
+
export interface DelegateAgentTaskInput {
|
|
11
|
+
platform: PlatformAdapter;
|
|
12
|
+
tool: string;
|
|
13
|
+
cwd: string;
|
|
14
|
+
promptText: string;
|
|
15
|
+
openIds: string[];
|
|
16
|
+
chatNamePrefix?: string;
|
|
17
|
+
msgTimestamp?: number;
|
|
18
|
+
traceId?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface DelegateAgentTaskResult {
|
|
22
|
+
chatId: string;
|
|
23
|
+
sessionId: string;
|
|
24
|
+
tool: string;
|
|
25
|
+
cwd: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function delegateAgentTask(input: DelegateAgentTaskInput): Promise<DelegateAgentTaskResult> {
|
|
29
|
+
const cwd = resolve(input.cwd);
|
|
30
|
+
const toolLabel = toolDisplayName(input.tool);
|
|
31
|
+
const init = await initClaudeSession(input.tool, cwd);
|
|
32
|
+
const sessionId = init.sessionId;
|
|
33
|
+
const chatNamePrefix = input.chatNamePrefix?.trim() || input.promptText.slice(0, 10) || "新会话";
|
|
34
|
+
const chatName = sessionChatName(chatNamePrefix, cwd);
|
|
35
|
+
|
|
36
|
+
let chatId: string;
|
|
37
|
+
try {
|
|
38
|
+
chatId = await input.platform.createGroup(chatName, input.openIds);
|
|
39
|
+
await input.platform.updateChatInfo(chatId, chatName, `${sessionPrefixForTool(input.tool)} ${sessionId}`);
|
|
40
|
+
await setDefaultCwd(cwd, chatId);
|
|
41
|
+
bindChatToSession(sessionId, chatId);
|
|
42
|
+
await recordSessionRegistry({
|
|
43
|
+
chatId,
|
|
44
|
+
sessionId,
|
|
45
|
+
tool: input.tool,
|
|
46
|
+
chatName,
|
|
47
|
+
turnCount: 0,
|
|
48
|
+
startTime: Date.now(),
|
|
49
|
+
running: false,
|
|
50
|
+
});
|
|
51
|
+
await saveSessionTool(sessionId, input.tool, chatName);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error(`[${ts()}] [AGENT-DELEGATE-TASK] create group failed: ${(err as Error).message}`);
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
await input.platform.sendCard(
|
|
58
|
+
chatId,
|
|
59
|
+
`${toolLabel} Session Ready`,
|
|
60
|
+
`已创建 **${toolLabel}** 会话群。\n\n` +
|
|
61
|
+
`**Session ID:** ${sessionId}\n` +
|
|
62
|
+
`**工作目录:** \`${cwd}\`\n\n` +
|
|
63
|
+
`下面会自动把任务作为第一句话发送给 ${toolLabel}。`,
|
|
64
|
+
"green",
|
|
65
|
+
).catch(() => {});
|
|
66
|
+
input.platform.setChatAvatar(chatId, input.tool, "new").catch(() => {});
|
|
67
|
+
|
|
68
|
+
await resumeAndPrompt(
|
|
69
|
+
sessionId,
|
|
70
|
+
input.promptText,
|
|
71
|
+
input.platform,
|
|
72
|
+
chatId,
|
|
73
|
+
input.msgTimestamp ?? Date.now(),
|
|
74
|
+
input.tool,
|
|
75
|
+
input.traceId,
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
console.log(`[${ts()}] [AGENT-DELEGATE-TASK] created ${toolLabel} session=${sessionId} chat=${chatId} cwd=${cwd}`);
|
|
79
|
+
return { chatId, sessionId, tool: input.tool, cwd };
|
|
80
|
+
}
|
|
81
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
|
|
3
|
+
import { getChatGptSubscriptionStatus } from "./chatgpt-subscription.ts";
|
|
4
|
+
|
|
5
|
+
export const CHATGPT_SUBSCRIPTION_PATH = "/api/chatgpt/subscription";
|
|
6
|
+
|
|
7
|
+
function jsonReply(res: ServerResponse, code: number, data: unknown): void {
|
|
8
|
+
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8" });
|
|
9
|
+
res.end(JSON.stringify(data));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function handleChatGptSubscriptionRequest(
|
|
13
|
+
req: IncomingMessage,
|
|
14
|
+
res: ServerResponse,
|
|
15
|
+
): Promise<boolean> {
|
|
16
|
+
const method = req.method ?? "GET";
|
|
17
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
18
|
+
if (url.pathname !== CHATGPT_SUBSCRIPTION_PATH) return false;
|
|
19
|
+
|
|
20
|
+
if (method !== "GET") {
|
|
21
|
+
jsonReply(res, 405, { ok: false, code: "method_not_allowed", reason: "Use GET." });
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
jsonReply(res, 200, await getChatGptSubscriptionStatus());
|
|
26
|
+
return true;
|
|
27
|
+
}
|