linkque-cli-v2 1.4.1 → 1.4.3
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/linkquejs.js +1162 -324
- package/package.json +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-XL6BPKTI.js → chunk-OMFQ2KRJ.js} +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-CUMXO6KK.js → chunk-SAQYWJAA.js} +4 -3
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-OHRCYI66.js → chunk-Y36JLYIE.js} +194 -36
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/chat.js +569 -216
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/clientCapability.js +2 -2
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/protocol.js +2 -2
- package/vendor/linkque-agent-appwrite-integrate/package.json +1 -1
- package/worker.js +284 -53
|
@@ -40,14 +40,15 @@ import {
|
|
|
40
40
|
resolveRunStorage,
|
|
41
41
|
toAgentRunMessages,
|
|
42
42
|
withIsoTimestamp
|
|
43
|
-
} from "../../chunk-
|
|
43
|
+
} from "../../chunk-SAQYWJAA.js";
|
|
44
44
|
import "../../chunk-IWOFD643.js";
|
|
45
45
|
import "../../chunk-PXCGFBV5.js";
|
|
46
46
|
import "../../chunk-ETG6PNHQ.js";
|
|
47
47
|
import {
|
|
48
48
|
getBakedProtocol
|
|
49
|
-
} from "../../chunk-
|
|
49
|
+
} from "../../chunk-OMFQ2KRJ.js";
|
|
50
50
|
import {
|
|
51
|
+
ACTIVATE_SKILL_TOOL_NAME,
|
|
51
52
|
CANCEL_CONVERSATION_RUN_OPERATION,
|
|
52
53
|
CONVERSATION_HISTORY_VERSION,
|
|
53
54
|
CardActionDiagnostics,
|
|
@@ -74,6 +75,7 @@ import {
|
|
|
74
75
|
ETelemetryCancelSource,
|
|
75
76
|
ETelemetryCommitStatus,
|
|
76
77
|
ETelemetryCompleteness,
|
|
78
|
+
ETelemetryContentSection,
|
|
77
79
|
ETelemetryCoverage,
|
|
78
80
|
ETelemetryErrorType,
|
|
79
81
|
ETelemetryEventName,
|
|
@@ -119,6 +121,7 @@ import {
|
|
|
119
121
|
createCardPresentationToolName,
|
|
120
122
|
createKnowledgeTools,
|
|
121
123
|
createPresentationDescription,
|
|
124
|
+
createRuntimeDebugLog,
|
|
122
125
|
createWebSearchTool,
|
|
123
126
|
encodeConversationSseEvent,
|
|
124
127
|
executeA2UICardResolver,
|
|
@@ -149,6 +152,7 @@ import {
|
|
|
149
152
|
projectPresentationReceiptForModel,
|
|
150
153
|
readJsonPointer,
|
|
151
154
|
readStructuredContent,
|
|
155
|
+
recordContentSection,
|
|
152
156
|
redactHeaders,
|
|
153
157
|
resolvePhoneAuthorizationDependency,
|
|
154
158
|
resolveWebSearchResource,
|
|
@@ -158,7 +162,7 @@ import {
|
|
|
158
162
|
summarizeBody,
|
|
159
163
|
warnLogFailure,
|
|
160
164
|
wrapToolWithHooks
|
|
161
|
-
} from "../../chunk-
|
|
165
|
+
} from "../../chunk-Y36JLYIE.js";
|
|
162
166
|
import "../../chunk-EAODRPW4.js";
|
|
163
167
|
import "../../chunk-XUFLSZM4.js";
|
|
164
168
|
import "../../chunk-GAYNLDWK.js";
|
|
@@ -7945,6 +7949,7 @@ import { randomUUID as randomUUID7 } from "node:crypto";
|
|
|
7945
7949
|
|
|
7946
7950
|
// ../linkque-agent-runtime/dist/esm/core/telemetry/toolObservation.js
|
|
7947
7951
|
import { randomUUID } from "node:crypto";
|
|
7952
|
+
var isSkillTool = (tool) => (tool.modelName ?? tool.name) === ACTIVATE_SKILL_TOOL_NAME;
|
|
7948
7953
|
function toolResultOutcome(result) {
|
|
7949
7954
|
try {
|
|
7950
7955
|
if (!(result && typeof result === "object")) {
|
|
@@ -7978,8 +7983,19 @@ function observeTool(tool, observation) {
|
|
|
7978
7983
|
detached: false,
|
|
7979
7984
|
links: []
|
|
7980
7985
|
});
|
|
7986
|
+
const captureBody = !isSkillTool(tool);
|
|
7981
7987
|
try {
|
|
7982
|
-
|
|
7988
|
+
if (captureBody) {
|
|
7989
|
+
recordContentSection(operation, ETelemetryContentSection.TOOL_INPUT, args);
|
|
7990
|
+
}
|
|
7991
|
+
const execContext = {
|
|
7992
|
+
...context ?? { toolCallId: "", emit: () => void 0 },
|
|
7993
|
+
observation: operation
|
|
7994
|
+
};
|
|
7995
|
+
const result = await tool.execute(args, signal, execContext);
|
|
7996
|
+
if (captureBody) {
|
|
7997
|
+
recordContentSection(operation, ETelemetryContentSection.TOOL_OUTPUT, result);
|
|
7998
|
+
}
|
|
7983
7999
|
operation.finish(toolResultOutcome(result), {
|
|
7984
8000
|
[ETelemetryAttribute.HARNESS_TOOL_RESULT_STATUS]: toolResultOutcome(result)
|
|
7985
8001
|
});
|
|
@@ -41941,11 +41957,36 @@ var AgentRuntime = class _AgentRuntime {
|
|
|
41941
41957
|
}
|
|
41942
41958
|
}, (observation) => this.runObservedAdvance(request, auth, options, observation), options.signal);
|
|
41943
41959
|
}
|
|
41960
|
+
/**
|
|
41961
|
+
* 记录本次用户输入(RFC §4.1):取进入 Runtime 的本次新消息中最后一条 user message
|
|
41962
|
+
* 的 content,作为 `linkque.debug.content.recorded` 的 body 走 record 链路落入
|
|
41963
|
+
* OTLP logs(body 含 section/value/availability)。不以模型 messages 上下文为输入;
|
|
41964
|
+
* 无 user 文本(卡片交互等)时按可用性如实标记,不伪造自然语言 Query。
|
|
41965
|
+
*/
|
|
41966
|
+
recordCurrentQuery(observation, request) {
|
|
41967
|
+
let lastUser;
|
|
41968
|
+
for (const message of request.messages) {
|
|
41969
|
+
if (message.role === "user")
|
|
41970
|
+
lastUser = message;
|
|
41971
|
+
}
|
|
41972
|
+
const content = lastUser?.content;
|
|
41973
|
+
const text2 = typeof content === "string" && content ? content : void 0;
|
|
41974
|
+
const card = content && typeof content === "object" && "type" in content && content.type === "a2ui.event" ? content : void 0;
|
|
41975
|
+
const displayText = card && "displayText" in card && typeof card.displayText === "string" && card.displayText ? card.displayText : void 0;
|
|
41976
|
+
const body = {
|
|
41977
|
+
section: ETelemetryContentSection.QUERY_INPUT,
|
|
41978
|
+
...text2 !== void 0 ? { value: text2 } : {},
|
|
41979
|
+
...card ? { inputType: "card", ...displayText ? { displayText } : {} } : {},
|
|
41980
|
+
availability: text2 !== void 0 || card ? "available" : "not_recorded"
|
|
41981
|
+
};
|
|
41982
|
+
observation.record(ETelemetryEventName.DEBUG_CONTENT_RECORDED, {}, body);
|
|
41983
|
+
}
|
|
41944
41984
|
async *runObservedAdvance(request, auth, options, observation) {
|
|
41945
41985
|
observation.record(ETelemetryEventName.TELEMETRY_COVERAGE, {
|
|
41946
41986
|
[ETelemetryAttribute.LINKQUE_TELEMETRY_COVERAGE]: this.agentLoop instanceof PiAgentLoop ? ETelemetryCoverage.PI_MODEL_TOOL_HOOK : ETelemetryCoverage.CORE_ONLY,
|
|
41947
41987
|
[ETelemetryAttribute.HARNESS_TELEMETRY_COMPLETENESS]: this.agentLoop instanceof PiAgentLoop ? ETelemetryCompleteness.COMPLETE : ETelemetryCompleteness.PARTIAL
|
|
41948
41988
|
});
|
|
41989
|
+
this.recordCurrentQuery(observation, request);
|
|
41949
41990
|
const cleanup = [];
|
|
41950
41991
|
let termination = ETelemetryStreamTermination.CANCEL;
|
|
41951
41992
|
let failed = false;
|
|
@@ -42262,6 +42303,12 @@ var AgentRuntime = class _AgentRuntime {
|
|
|
42262
42303
|
writing.finish(ETelemetryOutcome.SUCCESS, {
|
|
42263
42304
|
[ETelemetryAttribute.HARNESS_STATE_COMMIT_STATUS]: ETelemetryCommitStatus.COMMITTED
|
|
42264
42305
|
});
|
|
42306
|
+
if (this.followUpQuestionGenerationEnabled && options.onFollowUpQuestionSnapshot) {
|
|
42307
|
+
try {
|
|
42308
|
+
options.onFollowUpQuestionSnapshot(completedTurns.flatMap((turn) => (turn.role === "user" || turn.role === "assistant") && typeof turn.content === "string" ? [{ role: turn.role, content: turn.content }] : []));
|
|
42309
|
+
} catch {
|
|
42310
|
+
}
|
|
42311
|
+
}
|
|
42265
42312
|
} catch (error) {
|
|
42266
42313
|
writing.finish(ETelemetryOutcome.ERROR, {
|
|
42267
42314
|
[ETelemetryAttribute.HARNESS_STATE_COMMIT_STATUS]: ETelemetryCommitStatus.FAILED
|
|
@@ -43052,10 +43099,10 @@ function resolveLinkqueEndpoints(env, envSelector) {
|
|
|
43052
43099
|
}
|
|
43053
43100
|
function createAppBaseRuntime(protocol, env = process.env, ports = {}, logCtx) {
|
|
43054
43101
|
const log = (...a) => logCtx?.log?.("[runtime-factory]", ...a);
|
|
43055
|
-
const debugLog = logCtx?.log ? (line, scope) => {
|
|
43102
|
+
const debugLog = logCtx?.log ? createRuntimeDebugLog(new AppbaseRuntimeConfigProvider(env).get("LOG_LEVEL"), (line, scope) => {
|
|
43056
43103
|
const prefix = scope ? `[${scope}] ` : "";
|
|
43057
43104
|
logCtx.log?.(prefix + line);
|
|
43058
|
-
} : void 0;
|
|
43105
|
+
}) : void 0;
|
|
43059
43106
|
const runtimePorts = {};
|
|
43060
43107
|
if (ports.telemetry)
|
|
43061
43108
|
runtimePorts.telemetry = ports.telemetry;
|
|
@@ -43459,6 +43506,24 @@ function rowPermissions(ownerId) {
|
|
|
43459
43506
|
return [`read("${role}")`, `update("${role}")`, `delete("${role}")`];
|
|
43460
43507
|
}
|
|
43461
43508
|
|
|
43509
|
+
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/transactionRetry.js
|
|
43510
|
+
var TRANSACTION_RETRY_DELAYS_MS = [50, 100];
|
|
43511
|
+
async function retryTransactionConflict(operation) {
|
|
43512
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
43513
|
+
try {
|
|
43514
|
+
return await operation();
|
|
43515
|
+
} catch (error) {
|
|
43516
|
+
const delay = TRANSACTION_RETRY_DELAYS_MS[attempt];
|
|
43517
|
+
if (!isTransactionConflict(error) || delay === void 0)
|
|
43518
|
+
throw error;
|
|
43519
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
43520
|
+
}
|
|
43521
|
+
}
|
|
43522
|
+
}
|
|
43523
|
+
function isTransactionConflict(error) {
|
|
43524
|
+
return error?.code === 409;
|
|
43525
|
+
}
|
|
43526
|
+
|
|
43462
43527
|
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/conversationHistoryStore.js
|
|
43463
43528
|
var DEFAULT_HISTORY_OWNER_ID = "anonymous";
|
|
43464
43529
|
var RUN_CHUNK_QUERY_LIMIT = 5e3;
|
|
@@ -43526,54 +43591,58 @@ function createConversationHistoryStore(databases, config, ownerId = DEFAULT_HIS
|
|
|
43526
43591
|
await upsertDocument(databases, config.sessionDatabaseId, config.sessionTableId, request.threadId, metadata);
|
|
43527
43592
|
},
|
|
43528
43593
|
async completeRun(completion) {
|
|
43529
|
-
|
|
43530
|
-
|
|
43531
|
-
|
|
43532
|
-
|
|
43533
|
-
|
|
43534
|
-
|
|
43535
|
-
|
|
43536
|
-
|
|
43537
|
-
|
|
43538
|
-
|
|
43539
|
-
|
|
43540
|
-
|
|
43541
|
-
|
|
43542
|
-
|
|
43543
|
-
|
|
43544
|
-
|
|
43545
|
-
|
|
43546
|
-
|
|
43547
|
-
|
|
43548
|
-
|
|
43549
|
-
|
|
43550
|
-
|
|
43551
|
-
|
|
43552
|
-
|
|
43553
|
-
|
|
43554
|
-
|
|
43594
|
+
return retryTransactionConflict(async () => {
|
|
43595
|
+
const completedAt = completion.completedAt ?? Date.now();
|
|
43596
|
+
const transaction = await databases.createTransaction();
|
|
43597
|
+
const transactionId = transaction.$id;
|
|
43598
|
+
let status;
|
|
43599
|
+
try {
|
|
43600
|
+
const run = await readDocument(databases, config.runIndexDatabaseId, config.runIndexTableId, completion.runId, transactionId);
|
|
43601
|
+
if (!run || run.ownerId !== ownerId || run.threadId !== completion.threadId) {
|
|
43602
|
+
throw new ConversationHistoryNotFoundError("Run \u4E0D\u5C5E\u4E8E\u5F53\u524D\u7528\u6237");
|
|
43603
|
+
}
|
|
43604
|
+
const persistedStatus = readRunStatus(run.status);
|
|
43605
|
+
status = persistedStatus === "cancelling" || persistedStatus === "cancelled" ? "cancelled" : completion.status;
|
|
43606
|
+
await databases.updateDocument(config.runIndexDatabaseId, config.runIndexTableId, completion.runId, {
|
|
43607
|
+
status,
|
|
43608
|
+
completedAt,
|
|
43609
|
+
totalChunks: completion.totalChunks,
|
|
43610
|
+
...status === "cancelled" ? { cancelledAt: completedAt } : {}
|
|
43611
|
+
}, void 0, transactionId);
|
|
43612
|
+
await databases.updateTransaction(transactionId, true, false);
|
|
43613
|
+
} catch (error) {
|
|
43614
|
+
await rollbackTransactionQuietly(databases, transactionId);
|
|
43615
|
+
throw error;
|
|
43616
|
+
}
|
|
43617
|
+
const session = await readDocument(databases, config.sessionDatabaseId, config.sessionTableId, completion.threadId);
|
|
43618
|
+
if (session?.ownerId === ownerId && session.lastRunId === completion.runId) {
|
|
43619
|
+
await databases.updateDocument(config.sessionDatabaseId, config.sessionTableId, completion.threadId, { lastRunStatus: status });
|
|
43620
|
+
}
|
|
43621
|
+
});
|
|
43555
43622
|
},
|
|
43556
43623
|
async saveFollowUpQuestions(runId, threadId, value) {
|
|
43557
43624
|
const normalized = parseConversationFollowUpQuestions(value);
|
|
43558
43625
|
if (!normalized?.questions.length)
|
|
43559
43626
|
return;
|
|
43560
|
-
|
|
43561
|
-
|
|
43562
|
-
|
|
43563
|
-
|
|
43564
|
-
|
|
43565
|
-
|
|
43566
|
-
|
|
43567
|
-
|
|
43627
|
+
return retryTransactionConflict(async () => {
|
|
43628
|
+
const transaction = await databases.createTransaction();
|
|
43629
|
+
const transactionId = transaction.$id;
|
|
43630
|
+
try {
|
|
43631
|
+
const run = await readDocument(databases, config.runIndexDatabaseId, config.runIndexTableId, runId, transactionId);
|
|
43632
|
+
if (!run || run.ownerId !== ownerId || run.threadId !== threadId || readRunStatus(run.status) !== "done") {
|
|
43633
|
+
throw new ConversationHistoryNotFoundError("Run \u4E0D\u5C5E\u4E8E\u5F53\u524D\u7528\u6237\u6216\u5C1A\u672A\u6210\u529F\u7ED3\u675F");
|
|
43634
|
+
}
|
|
43635
|
+
if (run.blockedAnswer === true) {
|
|
43636
|
+
await databases.updateTransaction(transactionId, true, false);
|
|
43637
|
+
return;
|
|
43638
|
+
}
|
|
43639
|
+
await databases.updateDocument(config.runIndexDatabaseId, config.runIndexTableId, runId, { followUpQuestionsJson: JSON.stringify(normalized) }, void 0, transactionId);
|
|
43568
43640
|
await databases.updateTransaction(transactionId, true, false);
|
|
43569
|
-
|
|
43641
|
+
} catch (error) {
|
|
43642
|
+
await rollbackTransactionQuietly(databases, transactionId);
|
|
43643
|
+
throw error;
|
|
43570
43644
|
}
|
|
43571
|
-
|
|
43572
|
-
await databases.updateTransaction(transactionId, true, false);
|
|
43573
|
-
} catch (error) {
|
|
43574
|
-
await rollbackTransactionQuietly(databases, transactionId);
|
|
43575
|
-
throw error;
|
|
43576
|
-
}
|
|
43645
|
+
});
|
|
43577
43646
|
},
|
|
43578
43647
|
async getRecentFollowUpQuestionContext(agentId, threadId, currentRunId, limit) {
|
|
43579
43648
|
const boundedLimit = Math.max(0, Math.min(limit, 2));
|
|
@@ -43583,17 +43652,21 @@ function createConversationHistoryStore(databases, config, ownerId = DEFAULT_HIS
|
|
|
43583
43652
|
if (!currentRun || currentRun.ownerId !== ownerId || currentRun.agentId !== agentId || currentRun.threadId !== threadId) {
|
|
43584
43653
|
throw new ConversationHistoryNotFoundError("Run \u4E0D\u5C5E\u4E8E\u5F53\u524D\u7528\u6237");
|
|
43585
43654
|
}
|
|
43655
|
+
const currentStartedAt = readNonNegativeInteger2(currentRun.startedAt);
|
|
43656
|
+
if (!currentStartedAt)
|
|
43657
|
+
return [];
|
|
43586
43658
|
const result = await databases.listDocuments(config.runIndexDatabaseId, config.runIndexTableId, [
|
|
43587
43659
|
Query.equal("ownerId", ownerId),
|
|
43588
43660
|
Query.equal("agentId", agentId),
|
|
43589
43661
|
Query.equal("threadId", threadId),
|
|
43590
43662
|
Query.equal("historyVersion", CONVERSATION_HISTORY_VERSION),
|
|
43663
|
+
Query.lessThan("startedAt", currentStartedAt),
|
|
43591
43664
|
Query.orderDesc("startedAt"),
|
|
43592
43665
|
Query.limit(25)
|
|
43593
43666
|
]);
|
|
43594
43667
|
const entries = [];
|
|
43595
43668
|
for (const row of result.documents) {
|
|
43596
|
-
if (row.runId === currentRunId || readRunStatus(row.status) !== "done") {
|
|
43669
|
+
if (row.runId === currentRunId || readNonNegativeInteger2(row.startedAt) >= currentStartedAt || readRunStatus(row.status) !== "done") {
|
|
43597
43670
|
continue;
|
|
43598
43671
|
}
|
|
43599
43672
|
if (row.blockedQuestion === true || row.blockedAnswer === true)
|
|
@@ -43635,37 +43708,39 @@ function createConversationHistoryStore(databases, config, ownerId = DEFAULT_HIS
|
|
|
43635
43708
|
return status === "cancelling" || status === "cancelled" || readNonNegativeInteger2(run.cancelRequestedAt) > 0;
|
|
43636
43709
|
},
|
|
43637
43710
|
async markRunsRead(agentId, runIds) {
|
|
43638
|
-
|
|
43639
|
-
|
|
43640
|
-
|
|
43641
|
-
|
|
43642
|
-
|
|
43643
|
-
const
|
|
43644
|
-
|
|
43645
|
-
|
|
43646
|
-
|
|
43647
|
-
|
|
43648
|
-
|
|
43649
|
-
const batchReadAt = Date.now();
|
|
43650
|
-
const states = runs.map((run, index2) => ({
|
|
43651
|
-
runId: runIds[index2],
|
|
43652
|
-
isRead: true,
|
|
43653
|
-
readAt: readOptionalTimestamp(run?.readAt) ?? batchReadAt,
|
|
43654
|
-
threadId: requireRowString(run, "threadId"),
|
|
43655
|
-
newlyRead: readOptionalTimestamp(run?.readAt) === void 0
|
|
43656
|
-
}));
|
|
43657
|
-
await Promise.all(states.map((state2, index2) => {
|
|
43658
|
-
if (readOptionalTimestamp(runs[index2]?.readAt) !== void 0) {
|
|
43659
|
-
return Promise.resolve();
|
|
43711
|
+
return retryTransactionConflict(async () => {
|
|
43712
|
+
const transaction = await databases.createTransaction();
|
|
43713
|
+
const transactionId = transaction.$id;
|
|
43714
|
+
try {
|
|
43715
|
+
const runs = await Promise.all(runIds.map((runId) => readDocument(databases, config.runIndexDatabaseId, config.runIndexTableId, runId, transactionId)));
|
|
43716
|
+
const invalidRun = runs.some((run, index2) => {
|
|
43717
|
+
const expectedRunId = runIds[index2];
|
|
43718
|
+
return !run || run.$id !== expectedRunId || run.runId !== expectedRunId || run.ownerId !== ownerId || run.agentId !== agentId || run.historyVersion !== CONVERSATION_HISTORY_VERSION;
|
|
43719
|
+
});
|
|
43720
|
+
if (invalidRun) {
|
|
43721
|
+
throw new ConversationHistoryNotFoundError("Run \u4E0D\u5B58\u5728\u6216\u4E0D\u5C5E\u4E8E\u5F53\u524D\u7528\u6237");
|
|
43660
43722
|
}
|
|
43661
|
-
|
|
43662
|
-
|
|
43663
|
-
|
|
43664
|
-
|
|
43665
|
-
|
|
43666
|
-
|
|
43667
|
-
|
|
43668
|
-
|
|
43723
|
+
const batchReadAt = Date.now();
|
|
43724
|
+
const states = runs.map((run, index2) => ({
|
|
43725
|
+
runId: runIds[index2],
|
|
43726
|
+
isRead: true,
|
|
43727
|
+
readAt: readOptionalTimestamp(run?.readAt) ?? batchReadAt,
|
|
43728
|
+
threadId: requireRowString(run, "threadId"),
|
|
43729
|
+
newlyRead: readOptionalTimestamp(run?.readAt) === void 0
|
|
43730
|
+
}));
|
|
43731
|
+
await Promise.all(states.map((state2, index2) => {
|
|
43732
|
+
if (readOptionalTimestamp(runs[index2]?.readAt) !== void 0) {
|
|
43733
|
+
return Promise.resolve();
|
|
43734
|
+
}
|
|
43735
|
+
return databases.updateDocument(config.runIndexDatabaseId, config.runIndexTableId, state2.runId, { readAt: new Date(state2.readAt).toISOString() }, void 0, transactionId);
|
|
43736
|
+
}));
|
|
43737
|
+
await databases.updateTransaction(transactionId, true, false);
|
|
43738
|
+
return states;
|
|
43739
|
+
} catch (error) {
|
|
43740
|
+
await rollbackTransactionQuietly(databases, transactionId);
|
|
43741
|
+
throw error;
|
|
43742
|
+
}
|
|
43743
|
+
});
|
|
43669
43744
|
},
|
|
43670
43745
|
async isRunAnswerBlocked(agentId, runId) {
|
|
43671
43746
|
const run = await readDocument(databases, config.runIndexDatabaseId, config.runIndexTableId, runId);
|
|
@@ -43675,67 +43750,80 @@ function createConversationHistoryStore(databases, config, ownerId = DEFAULT_HIS
|
|
|
43675
43750
|
return run.blockedAnswer === true;
|
|
43676
43751
|
},
|
|
43677
43752
|
async replaceAnswer(input) {
|
|
43678
|
-
|
|
43679
|
-
|
|
43680
|
-
|
|
43681
|
-
|
|
43682
|
-
|
|
43683
|
-
|
|
43684
|
-
|
|
43685
|
-
|
|
43753
|
+
return retryTransactionConflict(async () => {
|
|
43754
|
+
const transaction = await databases.createTransaction();
|
|
43755
|
+
const transactionId = transaction.$id;
|
|
43756
|
+
try {
|
|
43757
|
+
const run = await readDocument(databases, config.runIndexDatabaseId, config.runIndexTableId, input.runId, transactionId);
|
|
43758
|
+
if (!run) {
|
|
43759
|
+
throw new ConversationHistoryNotFoundError("Run \u4E0D\u5B58\u5728");
|
|
43760
|
+
}
|
|
43761
|
+
if (run.blockedAnswer === true) {
|
|
43762
|
+
await databases.updateTransaction(transactionId, true, false);
|
|
43763
|
+
return "duplicate";
|
|
43764
|
+
}
|
|
43765
|
+
const active = !["done", "error", "cancelled"].includes(String(run.status));
|
|
43766
|
+
const now = Date.now();
|
|
43767
|
+
await databases.updateDocument(config.runIndexDatabaseId, config.runIndexTableId, input.runId, {
|
|
43768
|
+
blockedAnswer: true,
|
|
43769
|
+
answerReplacement: input.replacementText,
|
|
43770
|
+
blockedAt: now,
|
|
43771
|
+
blockedBy: input.eventId,
|
|
43772
|
+
followUpQuestionsJson: null,
|
|
43773
|
+
...active ? { status: "cancelling", cancelRequestedAt: now } : {}
|
|
43774
|
+
}, void 0, transactionId);
|
|
43686
43775
|
await databases.updateTransaction(transactionId, true, false);
|
|
43687
|
-
return "
|
|
43688
|
-
}
|
|
43689
|
-
|
|
43690
|
-
|
|
43691
|
-
|
|
43692
|
-
|
|
43693
|
-
answerReplacement: input.replacementText,
|
|
43694
|
-
blockedAt: now,
|
|
43695
|
-
blockedBy: input.eventId,
|
|
43696
|
-
followUpQuestionsJson: null,
|
|
43697
|
-
...active ? { status: "cancelling", cancelRequestedAt: now } : {}
|
|
43698
|
-
}, void 0, transactionId);
|
|
43699
|
-
await databases.updateTransaction(transactionId, true, false);
|
|
43700
|
-
return "applied";
|
|
43701
|
-
} catch (error) {
|
|
43702
|
-
await rollbackTransactionQuietly(databases, transactionId);
|
|
43703
|
-
throw error;
|
|
43704
|
-
}
|
|
43776
|
+
return "applied";
|
|
43777
|
+
} catch (error) {
|
|
43778
|
+
await rollbackTransactionQuietly(databases, transactionId);
|
|
43779
|
+
throw error;
|
|
43780
|
+
}
|
|
43781
|
+
});
|
|
43705
43782
|
},
|
|
43706
43783
|
async setRunBlocked(params) {
|
|
43707
|
-
|
|
43708
|
-
|
|
43709
|
-
|
|
43710
|
-
|
|
43711
|
-
|
|
43712
|
-
|
|
43713
|
-
|
|
43714
|
-
|
|
43715
|
-
|
|
43716
|
-
|
|
43717
|
-
|
|
43718
|
-
|
|
43719
|
-
|
|
43720
|
-
|
|
43721
|
-
|
|
43722
|
-
|
|
43723
|
-
|
|
43724
|
-
|
|
43725
|
-
|
|
43726
|
-
|
|
43727
|
-
|
|
43784
|
+
return retryTransactionConflict(async () => {
|
|
43785
|
+
const transaction = await databases.createTransaction();
|
|
43786
|
+
const transactionId = transaction.$id;
|
|
43787
|
+
try {
|
|
43788
|
+
const existing = await readDocument(databases, config.runIndexDatabaseId, config.runIndexTableId, params.runId, transactionId);
|
|
43789
|
+
if (!existing) {
|
|
43790
|
+
throw new ConversationHistoryNotFoundError("Run \u4E0D\u5B58\u5728");
|
|
43791
|
+
}
|
|
43792
|
+
const prevBlockedQuestion = existing.blockedQuestion === true;
|
|
43793
|
+
const prevBlockedAnswer = existing.blockedAnswer === true;
|
|
43794
|
+
const prevQuestionReplacement = readNonEmptyString(existing.questionReplacement);
|
|
43795
|
+
const prevAnswerReplacement = readNonEmptyString(existing.answerReplacement);
|
|
43796
|
+
const blockedQuestion = params.blockQuestion ?? prevBlockedQuestion;
|
|
43797
|
+
const blockedAnswer = params.blockAnswer ?? prevBlockedAnswer;
|
|
43798
|
+
const questionReplacement = params.blockQuestion === true ? params.questionReplacement ?? "" : params.blockQuestion === false ? null : prevQuestionReplacement ?? null;
|
|
43799
|
+
const answerReplacement = params.blockAnswer === true ? params.answerReplacement ?? "" : params.blockAnswer === false ? null : prevAnswerReplacement ?? null;
|
|
43800
|
+
const blockedAt = Date.now();
|
|
43801
|
+
const blockedBy = readNonEmptyString(params.blockedBy);
|
|
43802
|
+
await databases.updateDocument(config.runIndexDatabaseId, config.runIndexTableId, params.runId, {
|
|
43803
|
+
blockedQuestion,
|
|
43804
|
+
blockedAnswer,
|
|
43805
|
+
questionReplacement,
|
|
43806
|
+
answerReplacement,
|
|
43807
|
+
blockedAt,
|
|
43808
|
+
blockedBy: blockedBy ?? null,
|
|
43809
|
+
...blockedAnswer ? { followUpQuestionsJson: null } : {}
|
|
43810
|
+
}, void 0, transactionId);
|
|
43811
|
+
await databases.updateTransaction(transactionId, true, false);
|
|
43812
|
+
return {
|
|
43813
|
+
runId: params.runId,
|
|
43814
|
+
threadId: readNonEmptyString(existing.threadId) ?? params.threadId,
|
|
43815
|
+
blockedQuestion,
|
|
43816
|
+
blockedAnswer,
|
|
43817
|
+
questionReplacement: readNonEmptyString(questionReplacement),
|
|
43818
|
+
answerReplacement: readNonEmptyString(answerReplacement),
|
|
43819
|
+
blockedAt,
|
|
43820
|
+
blockedBy
|
|
43821
|
+
};
|
|
43822
|
+
} catch (error) {
|
|
43823
|
+
await rollbackTransactionQuietly(databases, transactionId);
|
|
43824
|
+
throw error;
|
|
43825
|
+
}
|
|
43728
43826
|
});
|
|
43729
|
-
return {
|
|
43730
|
-
runId: params.runId,
|
|
43731
|
-
threadId: readNonEmptyString(existing.threadId) ?? params.threadId,
|
|
43732
|
-
blockedQuestion,
|
|
43733
|
-
blockedAnswer,
|
|
43734
|
-
questionReplacement: readNonEmptyString(questionReplacement),
|
|
43735
|
-
answerReplacement: readNonEmptyString(answerReplacement),
|
|
43736
|
-
blockedAt,
|
|
43737
|
-
blockedBy
|
|
43738
|
-
};
|
|
43739
43827
|
},
|
|
43740
43828
|
async bootstrap(agentId, limit) {
|
|
43741
43829
|
const page2 = await listConversationPage(databases, config, agentId, ownerId, void 0, limit);
|
|
@@ -44144,28 +44232,30 @@ async function runReplaceAnswer(method, body, env, deps, logCtx, appwriteApiKey)
|
|
|
44144
44232
|
function createAppBaseStore(databases, databaseId, tableId, ownerId) {
|
|
44145
44233
|
const permissions = rowPermissions(ownerId);
|
|
44146
44234
|
return {
|
|
44147
|
-
create: async (row) => {
|
|
44148
|
-
const
|
|
44149
|
-
|
|
44150
|
-
tableId,
|
|
44151
|
-
"unique()",
|
|
44152
|
-
// 让 AppBase 生成唯一 rowId
|
|
44153
|
-
{
|
|
44235
|
+
create: async (row, documentId) => {
|
|
44236
|
+
const targetId = documentId ?? "unique()";
|
|
44237
|
+
try {
|
|
44238
|
+
const created = await databases.createDocument(databaseId, tableId, targetId, {
|
|
44154
44239
|
runId: row.runId,
|
|
44155
44240
|
threadId: row.threadId,
|
|
44156
44241
|
seqIndex: row.seqIndex,
|
|
44157
44242
|
status: row.status,
|
|
44158
44243
|
lines: row.lines,
|
|
44159
44244
|
...ownerId ? { ownerId } : {}
|
|
44160
|
-
},
|
|
44161
|
-
|
|
44162
|
-
)
|
|
44163
|
-
|
|
44245
|
+
}, permissions);
|
|
44246
|
+
return created.$id;
|
|
44247
|
+
} catch (error) {
|
|
44248
|
+
if (documentId && error?.code === 409) {
|
|
44249
|
+
return documentId;
|
|
44250
|
+
}
|
|
44251
|
+
throw error;
|
|
44252
|
+
}
|
|
44164
44253
|
}
|
|
44165
44254
|
};
|
|
44166
44255
|
}
|
|
44167
44256
|
|
|
44168
44257
|
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/chunkSink.js
|
|
44258
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
44169
44259
|
var ChunkSink = class {
|
|
44170
44260
|
storage;
|
|
44171
44261
|
chunkSize;
|
|
@@ -44225,11 +44315,27 @@ var ChunkSink = class {
|
|
|
44225
44315
|
});
|
|
44226
44316
|
}
|
|
44227
44317
|
/** 终结流:把剩余 pending 作为末 chunk create,带终态 status。 */
|
|
44228
|
-
async
|
|
44318
|
+
async flush() {
|
|
44319
|
+
await this.enqueue(() => this.flushPending());
|
|
44320
|
+
}
|
|
44321
|
+
async finalize(status, terminalChunk) {
|
|
44229
44322
|
this.closed = true;
|
|
44230
44323
|
this.clearFlushTimer();
|
|
44231
44324
|
await this.enqueue(async () => {
|
|
44232
|
-
|
|
44325
|
+
if (terminalChunk) {
|
|
44326
|
+
const lines = terminalChunk.split("\n");
|
|
44327
|
+
if (lines.at(-1) === "")
|
|
44328
|
+
lines.pop();
|
|
44329
|
+
this.pending.push(...lines);
|
|
44330
|
+
}
|
|
44331
|
+
const terminalId = createTerminalChunkId(this.ctx.runId);
|
|
44332
|
+
try {
|
|
44333
|
+
await this.createChunk(this.chunkCount, status, this.pending, terminalId);
|
|
44334
|
+
} catch (error) {
|
|
44335
|
+
if (!isUncertainWriteError(error))
|
|
44336
|
+
throw error;
|
|
44337
|
+
await this.createChunk(this.chunkCount, status, this.pending, terminalId);
|
|
44338
|
+
}
|
|
44233
44339
|
this.pending = [];
|
|
44234
44340
|
});
|
|
44235
44341
|
}
|
|
@@ -44283,21 +44389,31 @@ var ChunkSink = class {
|
|
|
44283
44389
|
return next;
|
|
44284
44390
|
}
|
|
44285
44391
|
/** create 一个 chunk;seqIndex 取当前 chunkCount,create 后 chunkCount 自增。 */
|
|
44286
|
-
async createChunk(seqIndex, status, lines) {
|
|
44392
|
+
async createChunk(seqIndex, status, lines, documentId) {
|
|
44287
44393
|
const id = await this.storage.create({
|
|
44288
44394
|
runId: this.ctx.runId,
|
|
44289
44395
|
threadId: this.ctx.threadId,
|
|
44290
44396
|
seqIndex,
|
|
44291
44397
|
status,
|
|
44292
44398
|
lines
|
|
44293
|
-
});
|
|
44399
|
+
}, documentId);
|
|
44294
44400
|
this.chunkCount += 1;
|
|
44295
44401
|
return id;
|
|
44296
44402
|
}
|
|
44297
44403
|
};
|
|
44404
|
+
function createTerminalChunkId(runId) {
|
|
44405
|
+
const digest = createHash2("sha256").update(runId).digest("hex");
|
|
44406
|
+
return `terminal_${digest.slice(0, 27)}`;
|
|
44407
|
+
}
|
|
44408
|
+
function isUncertainWriteError(error) {
|
|
44409
|
+
if (error instanceof TypeError)
|
|
44410
|
+
return true;
|
|
44411
|
+
const value = error;
|
|
44412
|
+
return value?.code === 0 || /timeout|timed out|network|socket|connection/i.test(value?.message ?? "");
|
|
44413
|
+
}
|
|
44298
44414
|
|
|
44299
44415
|
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/ports/appbaseSessionLease.js
|
|
44300
|
-
import { createHash as
|
|
44416
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
44301
44417
|
var DEFAULT_LEASE_TTL_MS = 20 * 60 * 1e3;
|
|
44302
44418
|
var TRANSACTION_TTL_SECONDS = 60;
|
|
44303
44419
|
function createSessionLease(persistence, options = {}) {
|
|
@@ -44305,7 +44421,7 @@ function createSessionLease(persistence, options = {}) {
|
|
|
44305
44421
|
const now = options.now ?? Date.now;
|
|
44306
44422
|
return {
|
|
44307
44423
|
acquire: async (threadId, ownerId) => {
|
|
44308
|
-
const leaseId =
|
|
44424
|
+
const leaseId = createSessionLeaseId(threadId);
|
|
44309
44425
|
if (await tryCreate(persistence, leaseId, ownerId))
|
|
44310
44426
|
return true;
|
|
44311
44427
|
const current = await persistence.read(leaseId);
|
|
@@ -44316,8 +44432,11 @@ function createSessionLease(persistence, options = {}) {
|
|
|
44316
44432
|
return persistence.replaceIfOwner(leaseId, current.ownerId, ownerId);
|
|
44317
44433
|
},
|
|
44318
44434
|
release: async (threadId, ownerId) => {
|
|
44319
|
-
const leaseId =
|
|
44320
|
-
await persistence.deleteIfOwner(leaseId, ownerId);
|
|
44435
|
+
const leaseId = createSessionLeaseId(threadId);
|
|
44436
|
+
const deleted = await persistence.deleteIfOwner(leaseId, ownerId);
|
|
44437
|
+
if (!deleted && (await persistence.read(leaseId))?.ownerId === ownerId) {
|
|
44438
|
+
throw new Error("\u4F1A\u8BDD\u79DF\u7EA6\u91CA\u653E\u672A\u63D0\u4EA4\uFF0C\u5F53\u524D\u8BF7\u6C42\u4ECD\u6301\u6709\u79DF\u7EA6");
|
|
44439
|
+
}
|
|
44321
44440
|
}
|
|
44322
44441
|
};
|
|
44323
44442
|
}
|
|
@@ -44368,8 +44487,8 @@ async function tryCreate(persistence, leaseId, ownerId) {
|
|
|
44368
44487
|
throw err2;
|
|
44369
44488
|
}
|
|
44370
44489
|
}
|
|
44371
|
-
function
|
|
44372
|
-
const digest =
|
|
44490
|
+
function createSessionLeaseId(threadId) {
|
|
44491
|
+
const digest = createHash3("sha256").update(threadId).digest("hex");
|
|
44373
44492
|
return `lease_${digest.slice(0, 30)}`;
|
|
44374
44493
|
}
|
|
44375
44494
|
async function mutateIfOwner(databases, databaseId, tableId, leaseId, expectedOwnerId, stageMutation) {
|
|
@@ -44926,8 +45045,132 @@ function errorMessage(error, fallback) {
|
|
|
44926
45045
|
return error instanceof Error && error.message.trim() ? error.message : fallback;
|
|
44927
45046
|
}
|
|
44928
45047
|
|
|
45048
|
+
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/conversationRunCompletion.js
|
|
45049
|
+
function createConversationRunCompletionCoordinator(databases, historyConfig, leaseConfig, historyOwnerId) {
|
|
45050
|
+
const ownerId = historyOwnerId ?? DEFAULT_HISTORY_OWNER_ID;
|
|
45051
|
+
return {
|
|
45052
|
+
completeAndRelease: async (completion) => {
|
|
45053
|
+
let transactionAttempts = 0;
|
|
45054
|
+
let databaseRequests = 0;
|
|
45055
|
+
let serialStages = 0;
|
|
45056
|
+
const status = await retryTransactionConflict(async () => {
|
|
45057
|
+
transactionAttempts += 1;
|
|
45058
|
+
databaseRequests += 1;
|
|
45059
|
+
serialStages += 1;
|
|
45060
|
+
const transaction = await databases.createTransaction({ ttl: 60 });
|
|
45061
|
+
const transactionId = transaction.$id;
|
|
45062
|
+
const leaseId = createSessionLeaseId(completion.threadId);
|
|
45063
|
+
let effectiveStatus;
|
|
45064
|
+
try {
|
|
45065
|
+
databaseRequests += 3;
|
|
45066
|
+
serialStages += 1;
|
|
45067
|
+
const [run, session, lease] = await Promise.all([
|
|
45068
|
+
readRequiredDocument(databases, historyConfig.runIndexDatabaseId, historyConfig.runIndexTableId, completion.runId, transactionId),
|
|
45069
|
+
readOptionalDocument(databases, historyConfig.sessionDatabaseId, historyConfig.sessionTableId, completion.threadId, transactionId),
|
|
45070
|
+
readRequiredDocument(databases, leaseConfig.databaseId, leaseConfig.tableId, leaseId, transactionId)
|
|
45071
|
+
]);
|
|
45072
|
+
if (run.ownerId !== ownerId || run.threadId !== completion.threadId) {
|
|
45073
|
+
throw new Error("Run \u4E0D\u5C5E\u4E8E\u5F53\u524D\u7528\u6237");
|
|
45074
|
+
}
|
|
45075
|
+
if (readLeaseOwner(lease) !== completion.leaseOwnerId) {
|
|
45076
|
+
throw new Error("\u4F1A\u8BDD\u79DF\u7EA6\u5DF2\u4E0D\u5C5E\u4E8E\u5F53\u524D\u8BF7\u6C42");
|
|
45077
|
+
}
|
|
45078
|
+
const persistedStatus = readTerminalStatus(run.status);
|
|
45079
|
+
effectiveStatus = persistedStatus === "cancelled" || run.status === "cancelling" ? "cancelled" : completion.status;
|
|
45080
|
+
const completedAt = completion.completedAt ?? Date.now();
|
|
45081
|
+
const operations = [
|
|
45082
|
+
{
|
|
45083
|
+
action: "update",
|
|
45084
|
+
databaseId: historyConfig.runIndexDatabaseId,
|
|
45085
|
+
collectionId: historyConfig.runIndexTableId,
|
|
45086
|
+
documentId: completion.runId,
|
|
45087
|
+
data: {
|
|
45088
|
+
status: effectiveStatus,
|
|
45089
|
+
completedAt,
|
|
45090
|
+
totalChunks: completion.totalChunks,
|
|
45091
|
+
...effectiveStatus === "cancelled" ? { cancelledAt: completedAt } : {}
|
|
45092
|
+
}
|
|
45093
|
+
},
|
|
45094
|
+
{
|
|
45095
|
+
action: "delete",
|
|
45096
|
+
databaseId: leaseConfig.databaseId,
|
|
45097
|
+
collectionId: leaseConfig.tableId,
|
|
45098
|
+
documentId: leaseId
|
|
45099
|
+
}
|
|
45100
|
+
];
|
|
45101
|
+
if (session?.ownerId === ownerId && session.lastRunId === completion.runId) {
|
|
45102
|
+
operations.push({
|
|
45103
|
+
action: "update",
|
|
45104
|
+
databaseId: historyConfig.sessionDatabaseId,
|
|
45105
|
+
collectionId: historyConfig.sessionTableId,
|
|
45106
|
+
documentId: completion.threadId,
|
|
45107
|
+
data: { lastRunStatus: effectiveStatus }
|
|
45108
|
+
});
|
|
45109
|
+
}
|
|
45110
|
+
databaseRequests += 1;
|
|
45111
|
+
serialStages += 1;
|
|
45112
|
+
await databases.createOperations({ transactionId, operations });
|
|
45113
|
+
databaseRequests += 1;
|
|
45114
|
+
serialStages += 1;
|
|
45115
|
+
await databases.updateTransaction({ transactionId, commit: true });
|
|
45116
|
+
return effectiveStatus;
|
|
45117
|
+
} catch (error) {
|
|
45118
|
+
databaseRequests += 1;
|
|
45119
|
+
serialStages += 1;
|
|
45120
|
+
await databases.updateTransaction({ transactionId, rollback: true }).catch(() => void 0);
|
|
45121
|
+
if (effectiveStatus && isCommitOutcomeUncertain(error)) {
|
|
45122
|
+
databaseRequests += 2;
|
|
45123
|
+
serialStages += 1;
|
|
45124
|
+
if (await isCompletionCommitted(databases, historyConfig, leaseConfig, completion, effectiveStatus, leaseId)) {
|
|
45125
|
+
return effectiveStatus;
|
|
45126
|
+
}
|
|
45127
|
+
}
|
|
45128
|
+
throw error;
|
|
45129
|
+
}
|
|
45130
|
+
});
|
|
45131
|
+
return { status, transactionAttempts, databaseRequests, serialStages };
|
|
45132
|
+
}
|
|
45133
|
+
};
|
|
45134
|
+
}
|
|
45135
|
+
async function isCompletionCommitted(databases, historyConfig, leaseConfig, completion, expectedStatus, leaseId) {
|
|
45136
|
+
const [run, lease] = await Promise.all([
|
|
45137
|
+
readOptionalDocument(databases, historyConfig.runIndexDatabaseId, historyConfig.runIndexTableId, completion.runId),
|
|
45138
|
+
readOptionalDocument(databases, leaseConfig.databaseId, leaseConfig.tableId, leaseId)
|
|
45139
|
+
]);
|
|
45140
|
+
return readTerminalStatus(run?.status) === expectedStatus && readLeaseOwner(lease) !== completion.leaseOwnerId;
|
|
45141
|
+
}
|
|
45142
|
+
async function readRequiredDocument(databases, databaseId, tableId, documentId, transactionId) {
|
|
45143
|
+
return await databases.getDocument({
|
|
45144
|
+
databaseId,
|
|
45145
|
+
collectionId: tableId,
|
|
45146
|
+
documentId,
|
|
45147
|
+
transactionId
|
|
45148
|
+
});
|
|
45149
|
+
}
|
|
45150
|
+
async function readOptionalDocument(databases, databaseId, tableId, documentId, transactionId) {
|
|
45151
|
+
try {
|
|
45152
|
+
return await readRequiredDocument(databases, databaseId, tableId, documentId, transactionId);
|
|
45153
|
+
} catch (error) {
|
|
45154
|
+
if (error?.code === 404)
|
|
45155
|
+
return void 0;
|
|
45156
|
+
throw error;
|
|
45157
|
+
}
|
|
45158
|
+
}
|
|
45159
|
+
function readLeaseOwner(row) {
|
|
45160
|
+
return Array.isArray(row?.turns) && typeof row.turns[0] === "string" ? row.turns[0] : void 0;
|
|
45161
|
+
}
|
|
45162
|
+
function readTerminalStatus(value) {
|
|
45163
|
+
return value === "done" || value === "error" || value === "cancelled" ? value : void 0;
|
|
45164
|
+
}
|
|
45165
|
+
function isCommitOutcomeUncertain(error) {
|
|
45166
|
+
if (error instanceof TypeError)
|
|
45167
|
+
return true;
|
|
45168
|
+
const value = error;
|
|
45169
|
+
return value?.code === 0 || /timeout|timed out|network|socket|connection/i.test(value?.message ?? "");
|
|
45170
|
+
}
|
|
45171
|
+
|
|
44929
45172
|
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/appbaseTelemetry.js
|
|
44930
|
-
import { createHash as
|
|
45173
|
+
import { createHash as createHash4 } from "node:crypto";
|
|
44931
45174
|
function createAppbaseTelemetry(options) {
|
|
44932
45175
|
const diagnostic = (code) => {
|
|
44933
45176
|
try {
|
|
@@ -44950,7 +45193,7 @@ function createAppbaseTelemetry(options) {
|
|
|
44950
45193
|
telemetry = new NoopRuntimeTelemetry();
|
|
44951
45194
|
}
|
|
44952
45195
|
const { authorizationHeader, mode } = options.gatewayAuth;
|
|
44953
|
-
const version =
|
|
45196
|
+
const version = createHash4("sha256").update(authorizationHeader).digest("hex");
|
|
44954
45197
|
return {
|
|
44955
45198
|
telemetry,
|
|
44956
45199
|
createContext(requestId) {
|
|
@@ -45111,6 +45354,7 @@ function resolveChatDeps(env, deps, logCtx, historyOwnerId, appwriteApiKey, envS
|
|
|
45111
45354
|
}
|
|
45112
45355
|
let store = deps?.store;
|
|
45113
45356
|
let sessionLease = deps?.sessionLease;
|
|
45357
|
+
let runCompletionCoordinator = deps?.runCompletionCoordinator;
|
|
45114
45358
|
let historyStore = deps?.historyStore;
|
|
45115
45359
|
let clientCapabilityStore = deps?.clientCapabilityStore;
|
|
45116
45360
|
let chunkSize = Number(APPBASE_RUNTIME_CONFIG_DEFAULTS.RUN_CHUNK_SIZE);
|
|
@@ -45133,7 +45377,8 @@ function resolveChatDeps(env, deps, logCtx, historyOwnerId, appwriteApiKey, envS
|
|
|
45133
45377
|
const config = new AppbaseRuntimeConfigProvider(env);
|
|
45134
45378
|
clientCapabilityStore ??= createClientCapabilityStore(handle.databases, config.get("CLIENT_CAPABILITY_DATABASE_ID"), config.get("CLIENT_CAPABILITY_TABLE_ID"), new TablesDB(handle.client));
|
|
45135
45379
|
try {
|
|
45136
|
-
|
|
45380
|
+
const historyStorage = resolveConversationHistoryStorage(env);
|
|
45381
|
+
historyStore ??= createConversationHistoryStore(handle.databases, historyStorage, historyOwnerId);
|
|
45137
45382
|
} catch (err2) {
|
|
45138
45383
|
logError("\u4F1A\u8BDD\u5386\u53F2\u5B58\u50A8\u914D\u7F6E\u7F3A\u5931:", err2);
|
|
45139
45384
|
return jsonError(500, "history_storage_unconfigured", err2 instanceof Error ? err2.message : "\u4F1A\u8BDD\u5386\u53F2\u5B58\u50A8\u914D\u7F6E\u7F3A\u5931");
|
|
@@ -45146,6 +45391,9 @@ function resolveChatDeps(env, deps, logCtx, historyOwnerId, appwriteApiKey, envS
|
|
|
45146
45391
|
if (!sessionLease && sessionLeaseStorage) {
|
|
45147
45392
|
sessionLease = createAppBaseSessionLease(handle.databases, sessionLeaseStorage.databaseId, sessionLeaseStorage.tableId, historyOwnerId);
|
|
45148
45393
|
}
|
|
45394
|
+
if (!runCompletionCoordinator && sessionLeaseStorage) {
|
|
45395
|
+
runCompletionCoordinator = createConversationRunCompletionCoordinator(handle.databases, resolveConversationHistoryStorage(env), sessionLeaseStorage, historyOwnerId);
|
|
45396
|
+
}
|
|
45149
45397
|
chunkSize = storage.chunkSize;
|
|
45150
45398
|
}
|
|
45151
45399
|
const mcpAuthorizationHeaderProvider = async () => resolved.authorizationHeader;
|
|
@@ -45196,6 +45444,7 @@ function resolveChatDeps(env, deps, logCtx, historyOwnerId, appwriteApiKey, envS
|
|
|
45196
45444
|
telemetry,
|
|
45197
45445
|
store,
|
|
45198
45446
|
sessionLease,
|
|
45447
|
+
runCompletionCoordinator,
|
|
45199
45448
|
historyStore,
|
|
45200
45449
|
clientCapabilityStore,
|
|
45201
45450
|
securityAnswerPlugin,
|
|
@@ -45489,7 +45738,7 @@ function wait2(ms, signal) {
|
|
|
45489
45738
|
}
|
|
45490
45739
|
|
|
45491
45740
|
// ../linkque-agent-appwrite-integrate/dist/esm/runtime/handler/chatExecution.js
|
|
45492
|
-
async function runAgentToChunks(deps, request, env, logCtx, authenticatedUserId, signal, traceId) {
|
|
45741
|
+
async function runAgentToChunks(deps, request, env, logCtx, authenticatedUserId, signal, traceId, beforeTerminal) {
|
|
45493
45742
|
const log = (...a) => logCtx?.log?.("[chat]", ...a);
|
|
45494
45743
|
const logError = (...a) => logCtx?.error?.("[chat]", ...a);
|
|
45495
45744
|
const debug = isDebugLogLevel(env);
|
|
@@ -45524,6 +45773,14 @@ async function runAgentToChunks(deps, request, env, logCtx, authenticatedUserId,
|
|
|
45524
45773
|
// 由 runtime 按已加载会话历史用户轮次 + 1 强准确推导(会话维度历史本就由本包维护)。
|
|
45525
45774
|
...traceId ? { hookContext: { traceId } } : {}
|
|
45526
45775
|
};
|
|
45776
|
+
let followUpTurns;
|
|
45777
|
+
if (request.executionScope !== "presentation") {
|
|
45778
|
+
runOptions.onFollowUpQuestionSnapshot = (turns) => {
|
|
45779
|
+
if (deps.securityAnswerPlugin?.isSecurityAnswerRun(request.runId))
|
|
45780
|
+
return;
|
|
45781
|
+
followUpTurns = turns.map((turn) => ({ ...turn }));
|
|
45782
|
+
};
|
|
45783
|
+
}
|
|
45527
45784
|
if (request.executionScope !== "presentation") {
|
|
45528
45785
|
runOptions.sessionId = request.threadId;
|
|
45529
45786
|
}
|
|
@@ -45557,12 +45814,18 @@ async function runAgentToChunks(deps, request, env, logCtx, authenticatedUserId,
|
|
|
45557
45814
|
debugLog("\u8C03\u7528 provider, provider=", deps.auth.provider, "model=", deps.model ?? deps.protocol.agent.model.defaultModel, "sessionId=", request.threadId);
|
|
45558
45815
|
let status = "done";
|
|
45559
45816
|
let loopErrorSeen = false;
|
|
45817
|
+
let terminalChunk;
|
|
45818
|
+
let completionError;
|
|
45560
45819
|
const assistantMessages = /* @__PURE__ */ new Map();
|
|
45561
45820
|
try {
|
|
45562
45821
|
const rawStream = deps.runtime.runAgentLoop(agentReq, deps.auth, runOptions);
|
|
45563
45822
|
const agentStream = debug ? debugLogAgentStream(rawStream, debugLog) : rawStream;
|
|
45564
45823
|
const sseStream = streamToConversationEvents(agentStream, { threadId: request.threadId, runId: request.runId }, signal ? { signal } : {});
|
|
45565
45824
|
for await (const ev of sseStream) {
|
|
45825
|
+
if (ev.event === "ag-ui" && ev.data.type === EventType.RUN_FINISHED) {
|
|
45826
|
+
terminalChunk = encodeConversationSseEvent(ev);
|
|
45827
|
+
continue;
|
|
45828
|
+
}
|
|
45566
45829
|
if (ev.event === "transport-error") {
|
|
45567
45830
|
loopErrorSeen = true;
|
|
45568
45831
|
logError("agent loop \u5931\u8D25\u4E8B\u4EF6, code=", ev.data.code, "message=", ev.data.message);
|
|
@@ -45599,19 +45862,44 @@ async function runAgentToChunks(deps, request, env, logCtx, authenticatedUserId,
|
|
|
45599
45862
|
}), { flush: true });
|
|
45600
45863
|
} finally {
|
|
45601
45864
|
try {
|
|
45602
|
-
await sink.
|
|
45865
|
+
await sink.flush();
|
|
45866
|
+
if (signal?.aborted && status === "done")
|
|
45867
|
+
status = "cancelled";
|
|
45868
|
+
try {
|
|
45869
|
+
status = await beforeTerminal?.(status, sink.totalChunks + 1) ?? status;
|
|
45870
|
+
} catch (error) {
|
|
45871
|
+
completionError = error;
|
|
45872
|
+
status = "error";
|
|
45873
|
+
terminalChunk = encodeConversationSseEvent({
|
|
45874
|
+
event: "transport-error",
|
|
45875
|
+
data: {
|
|
45876
|
+
code: "RUN_COMPLETION_FAILED",
|
|
45877
|
+
message: "\u4F1A\u8BDD\u6536\u5C3E\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002",
|
|
45878
|
+
retriable: true
|
|
45879
|
+
}
|
|
45880
|
+
});
|
|
45881
|
+
}
|
|
45882
|
+
const terminalStartedAt = Date.now();
|
|
45883
|
+
await sink.finalize(status, status === "done" || completionError ? terminalChunk : void 0);
|
|
45603
45884
|
log("chunk \u5DF2 finalize, status=", status, "\u603B chunk \u6570=", sink.totalChunks);
|
|
45885
|
+
log("\u7EC8\u6001\u53D1\u5E03\u5B8C\u6210:", {
|
|
45886
|
+
runId: request.runId,
|
|
45887
|
+
status,
|
|
45888
|
+
durationMs: Date.now() - terminalStartedAt
|
|
45889
|
+
});
|
|
45604
45890
|
} catch (err2) {
|
|
45605
45891
|
logError("finalize \u5931\u8D25:", err2);
|
|
45606
|
-
|
|
45892
|
+
completionError = err2;
|
|
45893
|
+
status = "error";
|
|
45607
45894
|
}
|
|
45608
45895
|
}
|
|
45609
45896
|
return {
|
|
45610
|
-
response: {
|
|
45897
|
+
response: completionError ? jsonError(500, "run_completion_failed", "\u4F1A\u8BDD\u6536\u5C3E\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002") : {
|
|
45611
45898
|
status: 201,
|
|
45612
45899
|
body: { runId: request.runId, threadId: request.threadId }
|
|
45613
45900
|
},
|
|
45614
45901
|
runStatus: status,
|
|
45902
|
+
...status === "done" && followUpTurns ? { followUpTurns } : {},
|
|
45615
45903
|
totalChunks: sink.totalChunks,
|
|
45616
45904
|
searchMessages: [...assistantMessages.entries()].flatMap(([itemId, message]) => message.content.trim() ? [{ itemId, content: message.content, sequence: message.sequence }] : []),
|
|
45617
45905
|
securityAnswer: deps.securityAnswerPlugin?.isSecurityAnswerRun(request.runId) ?? false
|
|
@@ -45725,9 +46013,14 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45725
46013
|
return true;
|
|
45726
46014
|
if (leaseReleased)
|
|
45727
46015
|
return true;
|
|
46016
|
+
const startedAt = Date.now();
|
|
45728
46017
|
try {
|
|
45729
46018
|
await cfg.sessionLease.release(request.threadId, leaseOwnerId);
|
|
45730
46019
|
leaseReleased = true;
|
|
46020
|
+
log("\u4F1A\u8BDD\u79DF\u7EA6\u5DF2\u91CA\u653E:", {
|
|
46021
|
+
runId: request.runId,
|
|
46022
|
+
durationMs: Date.now() - startedAt
|
|
46023
|
+
});
|
|
45731
46024
|
return true;
|
|
45732
46025
|
} catch (err2) {
|
|
45733
46026
|
logError("session \u79DF\u7EA6\u91CA\u653E\u5931\u8D25:", err2);
|
|
@@ -45753,31 +46046,59 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45753
46046
|
logError("\u4F1A\u8BDD\u76EE\u5F55\u521D\u59CB\u5316\u5931\u8D25:", err2);
|
|
45754
46047
|
return jsonError(500, "history_begin_failed", err2 instanceof Error ? err2.message : "\u4F1A\u8BDD\u76EE\u5F55\u521D\u59CB\u5316\u5931\u8D25");
|
|
45755
46048
|
}
|
|
45756
|
-
const userSearchContent = readSearchableUserContent(request);
|
|
45757
|
-
if (userSearchContent) {
|
|
45758
|
-
try {
|
|
45759
|
-
await cfg.historyStore.indexSearchMessages([
|
|
45760
|
-
{
|
|
45761
|
-
agentId: cfg.protocol.agentId,
|
|
45762
|
-
threadId: request.threadId,
|
|
45763
|
-
runId: request.runId,
|
|
45764
|
-
itemId: `${request.runId}:user`,
|
|
45765
|
-
role: "user",
|
|
45766
|
-
content: userSearchContent,
|
|
45767
|
-
createdAt: Date.now(),
|
|
45768
|
-
sequence: 0
|
|
45769
|
-
}
|
|
45770
|
-
]);
|
|
45771
|
-
} catch (err2) {
|
|
45772
|
-
logError("\u7528\u6237\u6D88\u606F\u641C\u7D22\u7D22\u5F15\u5199\u5165\u5931\u8D25:", err2);
|
|
45773
|
-
}
|
|
45774
|
-
}
|
|
45775
46049
|
}
|
|
45776
46050
|
const runAbortController = new AbortController();
|
|
45777
|
-
const
|
|
46051
|
+
const cancellationObserver = startRunCancellationObserver(isPresentationRun ? void 0 : cfg.historyStore, cfg.protocol.agentId, request.runId, runAbortController, env, logCtx);
|
|
45778
46052
|
let execution;
|
|
45779
46053
|
try {
|
|
45780
|
-
execution = await runAgentToChunks(cfg, request, env, logCtx, historyOwnerId, runAbortController.signal, traceId)
|
|
46054
|
+
execution = await runAgentToChunks(cfg, request, env, logCtx, historyOwnerId, runAbortController.signal, traceId, async (status, totalChunks) => {
|
|
46055
|
+
cancellationObserver.stop();
|
|
46056
|
+
if (runAbortController.signal.aborted && status === "done")
|
|
46057
|
+
status = "cancelled";
|
|
46058
|
+
const startedAt = Date.now();
|
|
46059
|
+
try {
|
|
46060
|
+
if (cfg.runCompletionCoordinator && cfg.historyStore && !isPresentationRun && leaseAcquired) {
|
|
46061
|
+
const completion = await cfg.runCompletionCoordinator.completeAndRelease({
|
|
46062
|
+
runId: request.runId,
|
|
46063
|
+
threadId: request.threadId,
|
|
46064
|
+
status,
|
|
46065
|
+
totalChunks,
|
|
46066
|
+
leaseOwnerId
|
|
46067
|
+
});
|
|
46068
|
+
status = completion.status;
|
|
46069
|
+
leaseReleased = true;
|
|
46070
|
+
log("\u4E3B\u4F1A\u8BDD\u5B8C\u6210\u4E8B\u52A1\u6307\u6807:", {
|
|
46071
|
+
runId: request.runId,
|
|
46072
|
+
transactionAttempts: completion.transactionAttempts,
|
|
46073
|
+
databaseRequests: completion.databaseRequests,
|
|
46074
|
+
serialStages: completion.serialStages
|
|
46075
|
+
});
|
|
46076
|
+
} else if (cfg.historyStore && !isPresentationRun) {
|
|
46077
|
+
await cfg.historyStore.completeRun({
|
|
46078
|
+
runId: request.runId,
|
|
46079
|
+
threadId: request.threadId,
|
|
46080
|
+
status,
|
|
46081
|
+
totalChunks
|
|
46082
|
+
});
|
|
46083
|
+
if (!await releaseLease())
|
|
46084
|
+
throw new Error("\u4F1A\u8BDD\u79DF\u7EA6\u91CA\u653E\u5931\u8D25");
|
|
46085
|
+
} else if (!await releaseLease()) {
|
|
46086
|
+
throw new Error("\u4F1A\u8BDD\u79DF\u7EA6\u91CA\u653E\u5931\u8D25");
|
|
46087
|
+
}
|
|
46088
|
+
log("\u4E3B\u4F1A\u8BDD\u5B8C\u6210\u4E8B\u52A1\u5DF2\u63D0\u4EA4:", {
|
|
46089
|
+
runId: request.runId,
|
|
46090
|
+
durationMs: Date.now() - startedAt
|
|
46091
|
+
});
|
|
46092
|
+
return status;
|
|
46093
|
+
} catch (error) {
|
|
46094
|
+
logError("\u4E3B\u4F1A\u8BDD\u6536\u5C3E\u5931\u8D25:", {
|
|
46095
|
+
runId: request.runId,
|
|
46096
|
+
message: error instanceof Error ? error.message : String(error),
|
|
46097
|
+
code: error?.code
|
|
46098
|
+
});
|
|
46099
|
+
throw error;
|
|
46100
|
+
}
|
|
46101
|
+
});
|
|
45781
46102
|
} catch (err2) {
|
|
45782
46103
|
logError("Agent \u6267\u884C\u5757\u5F02\u5E38\u9000\u51FA:", err2);
|
|
45783
46104
|
if (cfg.historyStore && !isPresentationRun) {
|
|
@@ -45794,18 +46115,45 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45794
46115
|
}
|
|
45795
46116
|
return jsonError(500, "run_execution_failed", err2 instanceof Error ? err2.message : "Agent \u6267\u884C\u5931\u8D25");
|
|
45796
46117
|
} finally {
|
|
45797
|
-
|
|
46118
|
+
cancellationObserver.stop();
|
|
46119
|
+
await cancellationObserver.settled;
|
|
45798
46120
|
}
|
|
45799
46121
|
if (cfg.historyStore && !isPresentationRun) {
|
|
45800
|
-
|
|
45801
|
-
|
|
45802
|
-
|
|
45803
|
-
|
|
45804
|
-
|
|
45805
|
-
|
|
45806
|
-
|
|
45807
|
-
|
|
45808
|
-
|
|
46122
|
+
if (execution.totalChunks === 0) {
|
|
46123
|
+
try {
|
|
46124
|
+
await cfg.historyStore.completeRun({
|
|
46125
|
+
runId: request.runId,
|
|
46126
|
+
threadId: request.threadId,
|
|
46127
|
+
status: "error",
|
|
46128
|
+
totalChunks: 0
|
|
46129
|
+
});
|
|
46130
|
+
} catch (error) {
|
|
46131
|
+
logError("\u7A7A run \u7EC8\u6001\u66F4\u65B0\u5931\u8D25:", error);
|
|
46132
|
+
}
|
|
46133
|
+
return execution.response;
|
|
46134
|
+
}
|
|
46135
|
+
}
|
|
46136
|
+
if (leaseAcquired && !leaseReleased)
|
|
46137
|
+
return execution.response;
|
|
46138
|
+
if (cfg.historyStore && !isPresentationRun) {
|
|
46139
|
+
const userSearchContent = readSearchableUserContent(request);
|
|
46140
|
+
if (userSearchContent) {
|
|
46141
|
+
try {
|
|
46142
|
+
await cfg.historyStore.indexSearchMessages([
|
|
46143
|
+
{
|
|
46144
|
+
agentId: cfg.protocol.agentId,
|
|
46145
|
+
threadId: request.threadId,
|
|
46146
|
+
runId: request.runId,
|
|
46147
|
+
itemId: `${request.runId}:user`,
|
|
46148
|
+
role: "user",
|
|
46149
|
+
content: userSearchContent,
|
|
46150
|
+
createdAt: Date.now(),
|
|
46151
|
+
sequence: 0
|
|
46152
|
+
}
|
|
46153
|
+
]);
|
|
46154
|
+
} catch (error) {
|
|
46155
|
+
logError("\u7528\u6237\u6D88\u606F\u641C\u7D22\u7D22\u5F15\u5199\u5165\u5931\u8D25:", error);
|
|
46156
|
+
}
|
|
45809
46157
|
}
|
|
45810
46158
|
if (execution.searchMessages.length) {
|
|
45811
46159
|
const createdAt = Date.now();
|
|
@@ -45840,16 +46188,16 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45840
46188
|
return true;
|
|
45841
46189
|
}
|
|
45842
46190
|
};
|
|
45843
|
-
let preparedFollowUpContext;
|
|
45844
46191
|
log("\u8FFD\u95EE\u89E6\u53D1\u68C0\u67E5:", {
|
|
45845
46192
|
runStatus: execution.runStatus,
|
|
45846
46193
|
securityAnswer: execution.securityAnswer,
|
|
45847
46194
|
assistantMessageCount: execution.searchMessages.length,
|
|
45848
46195
|
hasHistoryWriter: Boolean(cfg.historyStore?.saveFollowUpQuestions),
|
|
45849
|
-
|
|
46196
|
+
hasRunSnapshot: Boolean(execution.followUpTurns),
|
|
45850
46197
|
hasGenerator: Boolean(cfg.runtime.generateFollowUpQuestions)
|
|
45851
46198
|
});
|
|
45852
|
-
if (!isPresentationRun && !execution.securityAnswer &&
|
|
46199
|
+
if (!isPresentationRun && !execution.securityAnswer && execution.runStatus === "done" && execution.followUpTurns && cfg.historyStore?.saveFollowUpQuestions && cfg.runtime.generateFollowUpQuestions && !await isAnswerBlocked()) {
|
|
46200
|
+
const followUpStartedAt = Date.now();
|
|
45853
46201
|
let followUpHistory = [];
|
|
45854
46202
|
if (cfg.historyStore.getRecentFollowUpQuestionContext) {
|
|
45855
46203
|
try {
|
|
@@ -45859,19 +46207,14 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45859
46207
|
}
|
|
45860
46208
|
}
|
|
45861
46209
|
try {
|
|
45862
|
-
preparedFollowUpContext =
|
|
45863
|
-
log("\u8FFD\u95EE\u4E0A\u4E0B\u6587\
|
|
45864
|
-
|
|
45865
|
-
|
|
45866
|
-
historyCount: preparedFollowUpContext?.history?.length ?? 0
|
|
46210
|
+
const preparedFollowUpContext = prepareFollowUpQuestionContext(execution.followUpTurns, followUpHistory);
|
|
46211
|
+
log("\u8FFD\u95EE\u4E0A\u4E0B\u6587\u51C6\u5907\u5B8C\u6210:", {
|
|
46212
|
+
runId: request.runId,
|
|
46213
|
+
durationMs: Date.now() - followUpStartedAt
|
|
45867
46214
|
});
|
|
45868
|
-
|
|
45869
|
-
|
|
45870
|
-
|
|
45871
|
-
}
|
|
45872
|
-
const released = await releaseLease();
|
|
45873
|
-
if (released && preparedFollowUpContext && !await isAnswerBlocked() && cfg.historyStore?.saveFollowUpQuestions && cfg.runtime.generateFollowUpQuestions) {
|
|
45874
|
-
try {
|
|
46215
|
+
if (!preparedFollowUpContext || await isAnswerBlocked())
|
|
46216
|
+
return execution.response;
|
|
46217
|
+
const generationStartedAt = Date.now();
|
|
45875
46218
|
const questions = await cfg.runtime.generateFollowUpQuestions(preparedFollowUpContext, cfg.auth, {
|
|
45876
46219
|
runId: request.runId,
|
|
45877
46220
|
requestSource: EModelRequestSource.APPBASE_FUNCTION,
|
|
@@ -45881,8 +46224,12 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45881
46224
|
...historyOwnerId ? { userId: historyOwnerId } : {}
|
|
45882
46225
|
}
|
|
45883
46226
|
});
|
|
45884
|
-
log("\u8FFD\u95EE\u751F\u6210\u5B8C\u6210:", {
|
|
45885
|
-
|
|
46227
|
+
log("\u8FFD\u95EE\u751F\u6210\u5B8C\u6210:", {
|
|
46228
|
+
runId: request.runId,
|
|
46229
|
+
questionCount: questions.length,
|
|
46230
|
+
durationMs: Date.now() - generationStartedAt
|
|
46231
|
+
});
|
|
46232
|
+
if (questions.length > 0 && !await isAnswerBlocked()) {
|
|
45886
46233
|
await cfg.historyStore.saveFollowUpQuestions(request.runId, request.threadId, {
|
|
45887
46234
|
questions: questions.map((question) => ({
|
|
45888
46235
|
label: question,
|
|
@@ -45892,6 +46239,11 @@ async function runChat(method, bodyJSON, env, deps, logCtx, historyOwnerId, appw
|
|
|
45892
46239
|
}
|
|
45893
46240
|
} catch (err2) {
|
|
45894
46241
|
logError("\u8FFD\u95EE\u751F\u6210\u6216\u5199\u5165\u5931\u8D25:", err2);
|
|
46242
|
+
} finally {
|
|
46243
|
+
log("\u8FFD\u95EE\u65C1\u652F\u7ED3\u675F:", {
|
|
46244
|
+
runId: request.runId,
|
|
46245
|
+
durationMs: Date.now() - followUpStartedAt
|
|
46246
|
+
});
|
|
45895
46247
|
}
|
|
45896
46248
|
}
|
|
45897
46249
|
return execution.response;
|
|
@@ -45911,8 +46263,9 @@ function readSearchableUserContent(request) {
|
|
|
45911
46263
|
}
|
|
45912
46264
|
var DEFAULT_CANCEL_POLL_INTERVAL_MS = Number(APPBASE_RUNTIME_CONFIG_DEFAULTS.RUN_CANCEL_POLL_INTERVAL_MS);
|
|
45913
46265
|
function startRunCancellationObserver(store, agentId, runId, runAbortController, env, logCtx) {
|
|
45914
|
-
if (!store?.isRunCancellationRequested)
|
|
45915
|
-
return
|
|
46266
|
+
if (!store?.isRunCancellationRequested) {
|
|
46267
|
+
return { stop: () => void 0, settled: Promise.resolve() };
|
|
46268
|
+
}
|
|
45916
46269
|
const observerAbortController = new AbortController();
|
|
45917
46270
|
const intervalMs = readCancelPollInterval(env);
|
|
45918
46271
|
let queryErrorLogged = false;
|
|
@@ -45935,9 +46288,9 @@ function startRunCancellationObserver(store, agentId, runId, runAbortController,
|
|
|
45935
46288
|
await waitForCancellationPoll(intervalMs, observerAbortController.signal);
|
|
45936
46289
|
}
|
|
45937
46290
|
})();
|
|
45938
|
-
return
|
|
45939
|
-
observerAbortController.abort()
|
|
45940
|
-
|
|
46291
|
+
return {
|
|
46292
|
+
stop: () => observerAbortController.abort(),
|
|
46293
|
+
settled: task.catch(() => void 0)
|
|
45941
46294
|
};
|
|
45942
46295
|
}
|
|
45943
46296
|
function readCancelPollInterval(env) {
|