ai-project-manage-cli 4.0.16 → 4.0.17
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/dist/index.js +159 -103
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -154,6 +154,12 @@ var requestConfig = {
|
|
|
154
154
|
path: "/requirement-artifacts/delete"
|
|
155
155
|
})
|
|
156
156
|
},
|
|
157
|
+
theater: {
|
|
158
|
+
submitMemberResponse: defineEndpoint({
|
|
159
|
+
method: "POST",
|
|
160
|
+
path: "/theater/sessions/submit-member-response"
|
|
161
|
+
})
|
|
162
|
+
},
|
|
157
163
|
spaceWorkflow: {
|
|
158
164
|
aiStartNode: defineEndpoint({
|
|
159
165
|
method: "POST",
|
|
@@ -459,6 +465,10 @@ var EventSession = class {
|
|
|
459
465
|
return { type: "status", status: event.status, message: event.message };
|
|
460
466
|
}
|
|
461
467
|
}
|
|
468
|
+
/** 合并所有 assistant 片段,供剧场成员回传等场景使用 */
|
|
469
|
+
getAssistantText() {
|
|
470
|
+
return this.events.filter((e) => e.type === "assistant").map((e) => String(e.content ?? "")).join("\n").trim();
|
|
471
|
+
}
|
|
462
472
|
writeToFile(cwd, requirementId, agentId) {
|
|
463
473
|
const sessionsDir = resolve2(
|
|
464
474
|
cwd,
|
|
@@ -768,6 +778,151 @@ async function runPull(requirementId, workspaceDir) {
|
|
|
768
778
|
}
|
|
769
779
|
|
|
770
780
|
// src/commands/connect.ts
|
|
781
|
+
async function createAgentForJob(payload) {
|
|
782
|
+
const options = {
|
|
783
|
+
apiKey: payload.apiKey,
|
|
784
|
+
model: { id: payload.model ?? "default" },
|
|
785
|
+
local: { cwd: payload.cwd }
|
|
786
|
+
};
|
|
787
|
+
if (payload.agentId) {
|
|
788
|
+
return Agent.resume(payload.agentId, options);
|
|
789
|
+
}
|
|
790
|
+
return Agent.create(options);
|
|
791
|
+
}
|
|
792
|
+
async function runAgentStream(payload, session, agent) {
|
|
793
|
+
const run = await agent.send(payload.prompt);
|
|
794
|
+
let failed = false;
|
|
795
|
+
let failReason = "";
|
|
796
|
+
for await (const event of run.stream()) {
|
|
797
|
+
if (event.type === "status") {
|
|
798
|
+
console.log(`[Status]`, event.message || event.status);
|
|
799
|
+
if (event.status === "ERROR") {
|
|
800
|
+
failed = true;
|
|
801
|
+
failReason = event.message?.trim() ?? "";
|
|
802
|
+
console.error("[apm] Agent \u6267\u884C\u5931\u8D25:", failReason || "(\u65E0\u5931\u8D25\u539F\u56E0)");
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
if (event.type === "assistant") {
|
|
808
|
+
const output = event.message.content[0].text;
|
|
809
|
+
console.log(`[Output]`, output);
|
|
810
|
+
session.addEvent(event);
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
if (event.type === "thinking") {
|
|
814
|
+
console.log(`[Thinking]`, event.text);
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
if (event.type === "tool_call") {
|
|
818
|
+
if (event.status === "completed" || event.status === "error") {
|
|
819
|
+
console.log(`[ToolCall(${event.call_id}) Result]`);
|
|
820
|
+
}
|
|
821
|
+
session.addEvent(event);
|
|
822
|
+
continue;
|
|
823
|
+
}
|
|
824
|
+
if (event.type === "task") {
|
|
825
|
+
console.log(`[Task:${event.status}]`);
|
|
826
|
+
session.addEvent(event);
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
if (event.type === "request") {
|
|
830
|
+
console.log(JSON.stringify(event, null, 2));
|
|
831
|
+
session.addEvent(event);
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
console.log("\u672A\u77E5\u4E8B\u4EF6:", JSON.stringify(event, null, 2));
|
|
835
|
+
}
|
|
836
|
+
return { agentId: run.agentId, failed, failReason };
|
|
837
|
+
}
|
|
838
|
+
async function handleTheaterJob(api, payload) {
|
|
839
|
+
const sessionId = payload.theaterSessionId.trim();
|
|
840
|
+
const memberKey = payload.memberKey.trim();
|
|
841
|
+
if (!sessionId) {
|
|
842
|
+
throw new Error("THEATER_JOB \u7F3A\u5C11 theaterSessionId");
|
|
843
|
+
}
|
|
844
|
+
if (!memberKey) {
|
|
845
|
+
throw new Error("THEATER_JOB \u7F3A\u5C11 memberKey");
|
|
846
|
+
}
|
|
847
|
+
console.log("[apm] \u5267\u573A\u4F1A\u8BDD:", sessionId, "\u6210\u5458:", memberKey);
|
|
848
|
+
const eventSession = new EventSession(payload.prompt);
|
|
849
|
+
const agent = await createAgentForJob(payload);
|
|
850
|
+
const { agentId, failed, failReason } = await runAgentStream(
|
|
851
|
+
payload,
|
|
852
|
+
eventSession,
|
|
853
|
+
agent
|
|
854
|
+
);
|
|
855
|
+
const assistantText = eventSession.getAssistantText();
|
|
856
|
+
const content = failed ? `[Agent \u6267\u884C\u5931\u8D25] ${failReason || "\u672A\u77E5\u9519\u8BEF"}` : assistantText || "[Agent \u672A\u4EA7\u751F\u6587\u672C\u8F93\u51FA]";
|
|
857
|
+
const result = await api.theater.submitMemberResponse({
|
|
858
|
+
sessionId,
|
|
859
|
+
memberKey,
|
|
860
|
+
content,
|
|
861
|
+
agentId,
|
|
862
|
+
logId: payload.logId?.trim() || void 0
|
|
863
|
+
});
|
|
864
|
+
console.log(
|
|
865
|
+
"[apm] \u5267\u573A\u6210\u5458\u56DE\u590D\u5DF2\u63D0\u4EA4",
|
|
866
|
+
failed ? "(\u542B\u5931\u8D25\u8BF4\u660E)" : "",
|
|
867
|
+
result.clearedPending ? "\xB7 \u5DF2\u6E05\u9664\u5F85\u56DE\u590D" : ""
|
|
868
|
+
);
|
|
869
|
+
if (result.canAdvance) {
|
|
870
|
+
console.log("[apm] \u53EF\u624B\u52A8\u63A8\u8FDB\u4E3B\u6301\u8F6E\u6B21\uFF08autoPlay \u5173\u95ED\uFF09");
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
async function handleWorkflowJob(api, payload) {
|
|
874
|
+
const apmRoot = join6(payload.cwd, ".apm");
|
|
875
|
+
console.log("[apm] ROOT:", apmRoot);
|
|
876
|
+
console.log("[apm] workflow node:", payload.nodeId);
|
|
877
|
+
try {
|
|
878
|
+
await runPull(payload.requirementId, apmRoot);
|
|
879
|
+
} catch (pullErr) {
|
|
880
|
+
console.error("[apm] apm pull \u5931\u8D25:", pullErr);
|
|
881
|
+
throw pullErr;
|
|
882
|
+
}
|
|
883
|
+
const agent = await createAgentForJob(payload);
|
|
884
|
+
await api.cliRequirements.updateDevStatus({
|
|
885
|
+
requirementId: payload.requirementId,
|
|
886
|
+
status: "WORKING"
|
|
887
|
+
});
|
|
888
|
+
await api.spaceWorkflow.aiStartNode({
|
|
889
|
+
requirementId: payload.requirementId,
|
|
890
|
+
nodeId: payload.nodeId,
|
|
891
|
+
agentId: agent.agentId
|
|
892
|
+
});
|
|
893
|
+
const session = new EventSession(payload.prompt);
|
|
894
|
+
const { agentId, failed, failReason } = await runAgentStream(
|
|
895
|
+
payload,
|
|
896
|
+
session,
|
|
897
|
+
agent
|
|
898
|
+
);
|
|
899
|
+
await api.cliRequirements.updateDevStatus({
|
|
900
|
+
requirementId: payload.requirementId,
|
|
901
|
+
status: "IDLE"
|
|
902
|
+
});
|
|
903
|
+
session.writeToFile(payload.cwd, payload.requirementId, agentId);
|
|
904
|
+
if (failed) {
|
|
905
|
+
const failResult = await api.spaceWorkflow.aiFailNode({
|
|
906
|
+
requirementId: payload.requirementId,
|
|
907
|
+
nodeId: payload.nodeId,
|
|
908
|
+
error: failReason
|
|
909
|
+
});
|
|
910
|
+
console.log("[apm] \u5DE5\u4F5C\u6D41\u8282\u70B9\u72B6\u6001:", failResult.nodeStatus);
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
console.log("[Done]");
|
|
914
|
+
try {
|
|
915
|
+
await runUploadArtifact(payload.requirementId, apmRoot);
|
|
916
|
+
} catch (pullErr) {
|
|
917
|
+
console.error("[apm] apm upload-artifact \u5931\u8D25:", pullErr);
|
|
918
|
+
throw pullErr;
|
|
919
|
+
}
|
|
920
|
+
const confirmResult = await api.spaceWorkflow.aiConfirmNode({
|
|
921
|
+
requirementId: payload.requirementId,
|
|
922
|
+
nodeId: payload.nodeId
|
|
923
|
+
});
|
|
924
|
+
console.log("[apm] \u5DE5\u4F5C\u6D41\u8282\u70B9\u72B6\u6001:", confirmResult.nodeStatus);
|
|
925
|
+
}
|
|
771
926
|
function runConnect(opts) {
|
|
772
927
|
void (async () => {
|
|
773
928
|
const cfg = await ensureApmConfig();
|
|
@@ -806,113 +961,14 @@ function runConnect(opts) {
|
|
|
806
961
|
const text = typeof data === "string" ? data : data.toString();
|
|
807
962
|
try {
|
|
808
963
|
const msg = JSON.parse(text);
|
|
809
|
-
if (msg.type
|
|
964
|
+
if (msg.type === "THEATER_JOB") {
|
|
965
|
+
await handleTheaterJob(api, msg.payload);
|
|
810
966
|
return;
|
|
811
967
|
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
console.log("[apm] ROOT:", apmRoot);
|
|
815
|
-
console.log("[apm] workflow node:", payload.nodeId);
|
|
816
|
-
try {
|
|
817
|
-
await runPull(payload.requirementId, apmRoot);
|
|
818
|
-
} catch (pullErr) {
|
|
819
|
-
console.error("[apm] apm pull \u5931\u8D25:", pullErr);
|
|
820
|
-
throw pullErr;
|
|
821
|
-
}
|
|
822
|
-
const agent = await (async () => {
|
|
823
|
-
const options = {
|
|
824
|
-
apiKey: payload.apiKey,
|
|
825
|
-
model: { id: payload.model ?? "default" },
|
|
826
|
-
local: { cwd: payload.cwd }
|
|
827
|
-
};
|
|
828
|
-
if (payload.agentId) {
|
|
829
|
-
return Agent.resume(payload.agentId, options);
|
|
830
|
-
}
|
|
831
|
-
return Agent.create(options);
|
|
832
|
-
})();
|
|
833
|
-
await api.cliRequirements.updateDevStatus({
|
|
834
|
-
requirementId: payload.requirementId,
|
|
835
|
-
status: "WORKING"
|
|
836
|
-
});
|
|
837
|
-
await api.spaceWorkflow.aiStartNode({
|
|
838
|
-
requirementId: payload.requirementId,
|
|
839
|
-
nodeId: payload.nodeId,
|
|
840
|
-
agentId: agent.agentId
|
|
841
|
-
});
|
|
842
|
-
const session = new EventSession(payload.prompt);
|
|
843
|
-
const run = await agent.send(payload.prompt);
|
|
844
|
-
let agentRunFailed = false;
|
|
845
|
-
let agentFailReason = "";
|
|
846
|
-
for await (const event of run.stream()) {
|
|
847
|
-
if (event.type === "status") {
|
|
848
|
-
console.log(`[Status]`, event.message || event.status);
|
|
849
|
-
if (event.status === "ERROR") {
|
|
850
|
-
agentRunFailed = true;
|
|
851
|
-
agentFailReason = event.message?.trim() ?? "";
|
|
852
|
-
console.error(
|
|
853
|
-
"[apm] Agent \u6267\u884C\u5931\u8D25:",
|
|
854
|
-
agentFailReason || "(\u65E0\u5931\u8D25\u539F\u56E0)"
|
|
855
|
-
);
|
|
856
|
-
break;
|
|
857
|
-
}
|
|
858
|
-
continue;
|
|
859
|
-
}
|
|
860
|
-
if (event.type === "assistant") {
|
|
861
|
-
const output = event.message.content[0].text;
|
|
862
|
-
console.log(`[Output]`, output);
|
|
863
|
-
session.addEvent(event);
|
|
864
|
-
continue;
|
|
865
|
-
}
|
|
866
|
-
if (event.type === "thinking") {
|
|
867
|
-
const thinking = event.text;
|
|
868
|
-
console.log(`[Thinking]`, thinking);
|
|
869
|
-
continue;
|
|
870
|
-
}
|
|
871
|
-
if (event.type === "tool_call") {
|
|
872
|
-
if (event.status === "completed" || event.status === "error") {
|
|
873
|
-
console.log(`[ToolCall(${event.call_id}) Result]`);
|
|
874
|
-
}
|
|
875
|
-
session.addEvent(event);
|
|
876
|
-
continue;
|
|
877
|
-
}
|
|
878
|
-
if (event.type === "task") {
|
|
879
|
-
console.log(`[Task:${event.status}]`);
|
|
880
|
-
session.addEvent(event);
|
|
881
|
-
continue;
|
|
882
|
-
}
|
|
883
|
-
if (event.type === "request") {
|
|
884
|
-
console.log(JSON.stringify(event, null, 2));
|
|
885
|
-
session.addEvent(event);
|
|
886
|
-
continue;
|
|
887
|
-
}
|
|
888
|
-
console.log("\u672A\u77E5\u4E8B\u4EF6:", JSON.stringify(event, null, 2));
|
|
889
|
-
}
|
|
890
|
-
await api.cliRequirements.updateDevStatus({
|
|
891
|
-
requirementId: payload.requirementId,
|
|
892
|
-
status: "IDLE"
|
|
893
|
-
});
|
|
894
|
-
session.writeToFile(payload.cwd, payload.requirementId, run.agentId);
|
|
895
|
-
if (agentRunFailed) {
|
|
896
|
-
const failResult = await api.spaceWorkflow.aiFailNode({
|
|
897
|
-
requirementId: payload.requirementId,
|
|
898
|
-
nodeId: payload.nodeId,
|
|
899
|
-
error: agentFailReason
|
|
900
|
-
});
|
|
901
|
-
console.log("[apm] \u5DE5\u4F5C\u6D41\u8282\u70B9\u72B6\u6001:", failResult.nodeStatus);
|
|
968
|
+
if (msg.type === "JOB") {
|
|
969
|
+
await handleWorkflowJob(api, msg.payload);
|
|
902
970
|
return;
|
|
903
971
|
}
|
|
904
|
-
console.log("[Done]");
|
|
905
|
-
try {
|
|
906
|
-
await runUploadArtifact(payload.requirementId, apmRoot);
|
|
907
|
-
} catch (pullErr) {
|
|
908
|
-
console.error("[apm] apm upload-artifact \u5931\u8D25:", pullErr);
|
|
909
|
-
throw pullErr;
|
|
910
|
-
}
|
|
911
|
-
const confirmResult = await api.spaceWorkflow.aiConfirmNode({
|
|
912
|
-
requirementId: payload.requirementId,
|
|
913
|
-
nodeId: payload.nodeId
|
|
914
|
-
});
|
|
915
|
-
console.log("[apm] \u5DE5\u4F5C\u6D41\u8282\u70B9\u72B6\u6001:", confirmResult.nodeStatus);
|
|
916
972
|
} catch (err) {
|
|
917
973
|
console.error(
|
|
918
974
|
"[apm] \u65E0\u6CD5\u89E3\u6790 WebSocket \u6D88\u606F:",
|