linkque-cli-v2 1.3.4 → 1.3.6

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.
@@ -16,9 +16,11 @@ import {
16
16
  require_json_bigint
17
17
  } from "./chunk-ETG6PNHQ.js";
18
18
  import {
19
+ McpPhoneAuthorizationFailedError,
20
+ McpPhoneAuthorizationRequiredError,
19
21
  RUNTIME_CONFIG_DEFAULTS,
20
22
  RuntimeConfigProvider
21
- } from "./chunk-YSRA6KFZ.js";
23
+ } from "./chunk-RHFHCJYP.js";
22
24
  import {
23
25
  __commonJS,
24
26
  __require,
@@ -7358,8 +7360,6 @@ var APPBASE_RUNTIME_CONFIG_DEFAULTS = Object.freeze({
7358
7360
  SEARCH_TABLE_ID: "linkque_conversation_message_search",
7359
7361
  CLIENT_CAPABILITY_DATABASE_ID: "linkque",
7360
7362
  CLIENT_CAPABILITY_TABLE_ID: "linkque_client_cap_invocations",
7361
- A2UI_ACTION_DATABASE_ID: "linkque",
7362
- A2UI_ACTION_TABLE_ID: "linkque_a2ui_action_invocations",
7363
7363
  RUN_CHUNK_SIZE: "30",
7364
7364
  RUN_CANCEL_POLL_INTERVAL_MS: "500",
7365
7365
  LOG_LEVEL: "INFO"
@@ -11629,6 +11629,7 @@ _Operator.dateSubDays = (days) => new _Operator("dateSubDays", [days]).toString(
11629
11629
  _Operator.dateSetNow = () => new _Operator("dateSetNow", []).toString();
11630
11630
 
11631
11631
  // ../linkque-agent-appwrite-integrate/dist/esm/runtime/clientCapabilityStore.js
11632
+ import { createHash } from "node:crypto";
11632
11633
  var TRANSACTION_TTL_SECONDS = 60;
11633
11634
  var COMPLETE_MAX_ATTEMPTS = 3;
11634
11635
  function createClientCapabilityStore(databases, databaseId, tableId) {
@@ -11667,6 +11668,10 @@ function createClientCapabilityStore(databases, databaseId, tableId) {
11667
11668
  await rollbackTransaction(databases, transaction.$id);
11668
11669
  return { kind: "forbidden" };
11669
11670
  }
11671
+ if (!matchesCapability(current.capability, request.result)) {
11672
+ await rollbackTransaction(databases, transaction.$id);
11673
+ return { kind: "invalid" };
11674
+ }
11670
11675
  if (current.status === "expired" || Date.parse(current.acceptUntil) <= Date.now()) {
11671
11676
  if (current.status === "pending") {
11672
11677
  await databases.updateDocument({
@@ -11685,7 +11690,7 @@ function createClientCapabilityStore(databases, databaseId, tableId) {
11685
11690
  const serialized = serializeResult(request.result);
11686
11691
  if (current.status !== "pending") {
11687
11692
  await rollbackTransaction(databases, transaction.$id);
11688
- return current.result === serialized ? { kind: "accepted", repeated: true } : { kind: "conflict" };
11693
+ return current.result === serialized || current.result === consumedDigest(serialized) ? { kind: "accepted", repeated: true } : { kind: "conflict" };
11689
11694
  }
11690
11695
  await databases.updateDocument({
11691
11696
  databaseId,
@@ -11711,8 +11716,10 @@ function createClientCapabilityStore(databases, databaseId, tableId) {
11711
11716
  throw new Error("Failed to complete client capability result");
11712
11717
  },
11713
11718
  async consume(invocationId) {
11719
+ const current = await this.get(invocationId);
11714
11720
  await databases.updateDocument(databaseId, tableId, invocationId, {
11715
11721
  status: "consumed",
11722
+ result: current?.result ? consumedDigest(current.result) : void 0,
11716
11723
  consumedAt: (/* @__PURE__ */ new Date()).toISOString()
11717
11724
  });
11718
11725
  },
@@ -11752,6 +11759,15 @@ function parseStoredClientCapabilityResult(value) {
11752
11759
  function serializeResult(result) {
11753
11760
  return JSON.stringify(result);
11754
11761
  }
11762
+ function consumedDigest(serialized) {
11763
+ return `sha256:${createHash("sha256").update(serialized).digest("hex")}`;
11764
+ }
11765
+ function matchesCapability(capability, result) {
11766
+ if (!result.ok)
11767
+ return true;
11768
+ const data = isRecord(result.data) ? result.data : {};
11769
+ return capability === "location" ? Number.isFinite(data.latitude) && Number.isFinite(data.longitude) : typeof data.authCode === "string" && data.authCode.length > 0;
11770
+ }
11755
11771
  async function commitTransaction(databases, transactionId) {
11756
11772
  await databases.updateTransaction({ transactionId, commit: true });
11757
11773
  }
@@ -11762,7 +11778,7 @@ function readInvocation(value) {
11762
11778
  if (!value || typeof value !== "object" || Array.isArray(value))
11763
11779
  return void 0;
11764
11780
  const row = value;
11765
- if (typeof row.$id !== "string" || typeof row.ownerId !== "string" || typeof row.agentId !== "string" || typeof row.threadId !== "string" || typeof row.runId !== "string" || typeof row.toolCallId !== "string" || row.capability !== "location" || typeof row.status !== "string" || typeof row.input !== "string" || typeof row.createdAt !== "string" || typeof row.acceptUntil !== "string" || typeof row.expiresAt !== "string")
11781
+ if (typeof row.$id !== "string" || typeof row.ownerId !== "string" || typeof row.agentId !== "string" || typeof row.threadId !== "string" || typeof row.runId !== "string" || typeof row.toolCallId !== "string" || !["location", "userInfo"].includes(String(row.capability)) || typeof row.status !== "string" || typeof row.input !== "string" || typeof row.createdAt !== "string" || typeof row.acceptUntil !== "string" || typeof row.expiresAt !== "string")
11766
11782
  return void 0;
11767
11783
  return {
11768
11784
  invocationId: row.$id,
@@ -11771,7 +11787,7 @@ function readInvocation(value) {
11771
11787
  threadId: row.threadId,
11772
11788
  runId: row.runId,
11773
11789
  toolCallId: row.toolCallId,
11774
- capability: "location",
11790
+ capability: row.capability,
11775
11791
  status: row.status,
11776
11792
  input: row.input,
11777
11793
  ...typeof row.result === "string" ? { result: row.result } : {},
@@ -11781,6 +11797,9 @@ function readInvocation(value) {
11781
11797
  ...typeof row.consumedAt === "string" ? { consumedAt: row.consumedAt } : {}
11782
11798
  };
11783
11799
  }
11800
+ function isRecord(value) {
11801
+ return Boolean(value && typeof value === "object" && !Array.isArray(value));
11802
+ }
11784
11803
  function isNotFound(error) {
11785
11804
  return Boolean(error && typeof error === "object" && "code" in error && error.code === 404);
11786
11805
  }
@@ -11894,138 +11913,6 @@ function parsePositiveInt(raw, name) {
11894
11913
  return n;
11895
11914
  }
11896
11915
 
11897
- // ../linkque-agent-appwrite-integrate/dist/esm/runtime/paymentActionStore.js
11898
- import { createHash } from "node:crypto";
11899
- var PAYMENT_INVOCATION_TTL_MS = 24 * 60 * 60 * 1e3;
11900
- function createPaymentInvocation(ownerId, request, now = /* @__PURE__ */ new Date()) {
11901
- const invocationId = createPaymentInvocationId(ownerId, request);
11902
- return {
11903
- invocationId,
11904
- ownerId,
11905
- agentId: request.agentId,
11906
- threadId: request.threadId,
11907
- cardRef: request.source.cardRef,
11908
- cardRevision: request.source.cardRevision,
11909
- cardInstanceId: request.source.cardInstanceId,
11910
- surfaceId: request.source.surfaceId,
11911
- componentId: request.source.componentId,
11912
- input: request.input,
11913
- status: "executing",
11914
- createdAt: now.toISOString(),
11915
- expiresAt: new Date(now.getTime() + PAYMENT_INVOCATION_TTL_MS).toISOString()
11916
- };
11917
- }
11918
- function createPaymentInvocationId(ownerId, request) {
11919
- const material = stableStringify({
11920
- ownerId,
11921
- agentId: request.agentId,
11922
- threadId: request.threadId,
11923
- cardInstanceId: request.source.cardInstanceId,
11924
- cardRevision: request.source.cardRevision,
11925
- surfaceId: request.source.surfaceId,
11926
- componentId: request.source.componentId,
11927
- input: request.input
11928
- });
11929
- return `pa_${createHash("sha256").update(material).digest("hex").slice(0, 32)}`;
11930
- }
11931
- function createPaymentActionStore(databases, databaseId, tableId) {
11932
- return {
11933
- async begin(invocation) {
11934
- const { invocationId, input, ...fields } = invocation;
11935
- try {
11936
- const document = await databases.createDocument(databaseId, tableId, invocationId, { ...fields, input: JSON.stringify(input) }, []);
11937
- return {
11938
- kind: "created",
11939
- invocation: readInvocation2(document) ?? invocation
11940
- };
11941
- } catch (error) {
11942
- if (!isConflict2(error))
11943
- throw error;
11944
- const existing = await this.get(invocationId);
11945
- if (!existing)
11946
- throw error;
11947
- return { kind: "existing", invocation: existing };
11948
- }
11949
- },
11950
- async get(invocationId) {
11951
- try {
11952
- return readInvocation2(await databases.getDocument(databaseId, tableId, invocationId));
11953
- } catch (error) {
11954
- if (isNotFound2(error))
11955
- return void 0;
11956
- throw error;
11957
- }
11958
- },
11959
- async update(invocationId, patch) {
11960
- const data = { ...patch };
11961
- const document = await databases.updateDocument(databaseId, tableId, invocationId, data);
11962
- const invocation = readInvocation2(document);
11963
- if (!invocation)
11964
- throw new Error("\u652F\u4ED8 invocation \u6301\u4E45\u5316\u7ED3\u679C\u65E0\u6548");
11965
- return invocation;
11966
- },
11967
- async cleanup(now) {
11968
- const result = await databases.listDocuments(databaseId, tableId, [
11969
- Query.lessThanEqual("expiresAt", now),
11970
- Query.limit(500)
11971
- ]);
11972
- for (const document of result.documents) {
11973
- await databases.deleteDocument(databaseId, tableId, document.$id);
11974
- }
11975
- return result.documents.length;
11976
- }
11977
- };
11978
- }
11979
- function readInvocation2(value) {
11980
- if (!value || typeof value !== "object" || Array.isArray(value))
11981
- return void 0;
11982
- const row = value;
11983
- if (typeof row.$id !== "string" || typeof row.ownerId !== "string" || typeof row.agentId !== "string" || typeof row.threadId !== "string" || typeof row.cardRef !== "string" || typeof row.cardRevision !== "string" || typeof row.cardInstanceId !== "string" || typeof row.surfaceId !== "string" || typeof row.componentId !== "string" || typeof row.input !== "string" || typeof row.status !== "string" || typeof row.createdAt !== "string" || typeof row.expiresAt !== "string")
11984
- return void 0;
11985
- const input = parseJsonObject(row.input);
11986
- if (!input)
11987
- return void 0;
11988
- return {
11989
- invocationId: row.$id,
11990
- ownerId: row.ownerId,
11991
- agentId: row.agentId,
11992
- threadId: row.threadId,
11993
- cardRef: row.cardRef,
11994
- cardRevision: row.cardRevision,
11995
- cardInstanceId: row.cardInstanceId,
11996
- surfaceId: row.surfaceId,
11997
- componentId: row.componentId,
11998
- input,
11999
- status: row.status,
12000
- ...typeof row.tradeNO === "string" ? { tradeNO: row.tradeNO } : {},
12001
- createdAt: row.createdAt,
12002
- expiresAt: row.expiresAt
12003
- };
12004
- }
12005
- function parseJsonObject(value) {
12006
- try {
12007
- const parsed = JSON.parse(value);
12008
- return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : void 0;
12009
- } catch {
12010
- return void 0;
12011
- }
12012
- }
12013
- function stableStringify(value) {
12014
- if (Array.isArray(value))
12015
- return `[${value.map(stableStringify).join(",")}]`;
12016
- if (value && typeof value === "object") {
12017
- return `{${Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, item]) => `${JSON.stringify(key)}:${stableStringify(item)}`).join(",")}}`;
12018
- }
12019
- return JSON.stringify(value);
12020
- }
12021
- function isNotFound2(error) {
12022
- return Boolean(error && typeof error === "object" && "code" in error && error.code === 404);
12023
- }
12024
- function isConflict2(error) {
12025
- const value = error;
12026
- return value?.code === 409 || /already exists|conflict|duplicate|409/i.test(value?.message ?? "");
12027
- }
12028
-
12029
11916
  // ../linkque-agent-appwrite-integrate/dist/esm/runtime/handler/handlerUtils.js
12030
11917
  function resolveBodyJSON(req) {
12031
11918
  if (req.bodyJSON !== void 0 && typeof req.bodyJSON !== "string") {
@@ -12070,6 +11957,52 @@ var TRIGGER_HEADER = "x-appwrite-trigger";
12070
11957
  var ENV_SELECTOR_HEADER = "x-linkque-env";
12071
11958
  var TRACE_ID_HEADER = "x-trace-id";
12072
11959
  var TEMPORARY_FRONTEND_APP_ID_HEADER = "x-linkque-alipay-app-id";
11960
+ var ALIPAY_AUTH_CODE_HEADER = "x-alipay-authcode";
11961
+ var AUTH_UNAVAILABLE_HEADER = "x-linkque-phone-auth-unavailable";
11962
+ function readMcpPhoneAuthorization(headers) {
11963
+ const authCode = readHeader(headers, ALIPAY_AUTH_CODE_HEADER)?.trim();
11964
+ if (authCode && authCode.length <= 4096)
11965
+ return { kind: "authorized", authCode };
11966
+ if (readHeader(headers, AUTH_UNAVAILABLE_HEADER) === "1")
11967
+ return { kind: "unavailable" };
11968
+ return void 0;
11969
+ }
11970
+ function mcpPhoneAuthorizationChallenge(error) {
11971
+ if (error instanceof McpPhoneAuthorizationFailedError) {
11972
+ return {
11973
+ status: 403,
11974
+ body: {
11975
+ ok: false,
11976
+ error: { code: error.code, message: error.message, retryable: true }
11977
+ }
11978
+ };
11979
+ }
11980
+ if (!(error instanceof McpPhoneAuthorizationRequiredError))
11981
+ return void 0;
11982
+ return {
11983
+ status: 428,
11984
+ body: {
11985
+ error: { code: error.code, message: error.message, retryable: true },
11986
+ authorization: {
11987
+ capability: "userInfo",
11988
+ scopes: ["auth_user"],
11989
+ required: error.required
11990
+ }
11991
+ }
11992
+ };
11993
+ }
11994
+ function readHeader(headers, name) {
11995
+ if (!headers)
11996
+ return void 0;
11997
+ const direct = headValue(headers[name]);
11998
+ if (direct !== void 0)
11999
+ return direct;
12000
+ for (const [key, value] of Object.entries(headers)) {
12001
+ if (key.toLowerCase() === name)
12002
+ return headValue(value);
12003
+ }
12004
+ return void 0;
12005
+ }
12073
12006
  function readEnvSelector(headers) {
12074
12007
  if (!headers)
12075
12008
  return "PROD";
@@ -12197,6 +12130,9 @@ async function runClientCapabilityResult(method, body, ownerId, store) {
12197
12130
  if (outcome.kind === "conflict") {
12198
12131
  return jsonError(409, "result_conflict", "\u8BE5\u8C03\u7528\u5DF2\u6709\u4E0D\u540C\u7ED3\u679C");
12199
12132
  }
12133
+ if (outcome.kind === "invalid") {
12134
+ return jsonError(400, "invalid_result", "\u7AEF\u4FA7\u80FD\u529B\u7ED3\u679C\u4E0E\u8BF7\u6C42\u4E0D\u5339\u914D");
12135
+ }
12200
12136
  if (outcome.kind === "expired" || outcome.kind === "not_found") {
12201
12137
  return jsonError(410, "invocation_expired", "\u8BE5\u7AEF\u4FA7\u8C03\u7528\u5DF2\u5931\u6548");
12202
12138
  }
@@ -12214,13 +12150,6 @@ async function clientCapability(ctx) {
12214
12150
  const store = createClientCapabilityStore(handle.databases, config.get("CLIENT_CAPABILITY_DATABASE_ID"), config.get("CLIENT_CAPABILITY_TABLE_ID"));
12215
12151
  const trigger = readAppwriteTrigger(ctx.req.headers);
12216
12152
  result = await runClientCapability(trigger, ctx.req.method, resolveBodyJSON(ctx.req), readAuthenticatedUserId(ctx.req.headers), store);
12217
- if (trigger === "schedule") {
12218
- const paymentDeleted = await createPaymentActionStore(handle.databases, config.get("A2UI_ACTION_DATABASE_ID"), config.get("A2UI_ACTION_TABLE_ID")).cleanup((/* @__PURE__ */ new Date()).toISOString());
12219
- result = {
12220
- status: 200,
12221
- body: { ...result.body, paymentDeleted }
12222
- };
12223
- }
12224
12153
  } catch (error) {
12225
12154
  ctx.error?.(error);
12226
12155
  result = error instanceof LinkqueGatewayAuthError || error instanceof LinkqueGatewayAuthUnconfiguredError ? jsonError(error.status, error.code, error.message) : jsonError(500, "internal_error", error instanceof Error ? error.message : "unknown error");
@@ -12228,17 +12157,17 @@ async function clientCapability(ctx) {
12228
12157
  return ctx.res.json?.(result.body, result.status);
12229
12158
  }
12230
12159
  function parseResultRequest(value) {
12231
- if (!isRecord(value) || value.version !== "1.0")
12160
+ if (!isRecord2(value) || value.version !== "1.0")
12232
12161
  return void 0;
12233
- if (!nonEmpty(value.invocationId) || !nonEmpty(value.runId) || !isRecord(value.result)) {
12162
+ if (!nonEmpty(value.invocationId) || !nonEmpty(value.runId) || !isRecord2(value.result)) {
12234
12163
  return void 0;
12235
12164
  }
12236
12165
  if (value.result.ok === true) {
12237
- if (!isLocation(value.result.data))
12166
+ if (!isLocation(value.result.data) && !isUserInfo(value.result.data))
12238
12167
  return void 0;
12239
12168
  } else if (value.result.ok === false) {
12240
12169
  const error = value.result.error;
12241
- if (!isRecord(error) || !nonEmpty(error.code) || !nonEmpty(error.message) || typeof error.retriable !== "boolean") {
12170
+ if (!isRecord2(error) || !nonEmpty(error.code) || !nonEmpty(error.message) || typeof error.retriable !== "boolean") {
12242
12171
  return void 0;
12243
12172
  }
12244
12173
  } else
@@ -12246,9 +12175,12 @@ function parseResultRequest(value) {
12246
12175
  return value;
12247
12176
  }
12248
12177
  function isLocation(value) {
12249
- return isRecord(value) && Number.isFinite(value.latitude) && Number.isFinite(value.longitude);
12178
+ return isRecord2(value) && Number.isFinite(value.latitude) && Number.isFinite(value.longitude);
12250
12179
  }
12251
- function isRecord(value) {
12180
+ function isUserInfo(value) {
12181
+ return isRecord2(value) && nonEmpty(value.authCode) && value.authCode.length <= 4096;
12182
+ }
12183
+ function isRecord2(value) {
12252
12184
  return Boolean(value && typeof value === "object" && !Array.isArray(value));
12253
12185
  }
12254
12186
  function nonEmpty(value) {
@@ -12265,6 +12197,8 @@ export {
12265
12197
  resolveBodyJSON,
12266
12198
  toAgentRunMessages,
12267
12199
  jsonError,
12200
+ readMcpPhoneAuthorization,
12201
+ mcpPhoneAuthorizationChallenge,
12268
12202
  readEnvSelector,
12269
12203
  readTraceId,
12270
12204
  readTemporaryFrontendAppId,
@@ -12278,8 +12212,6 @@ export {
12278
12212
  createAppBaseClient,
12279
12213
  createClientCapabilityStore,
12280
12214
  parseStoredClientCapabilityResult,
12281
- createPaymentInvocation,
12282
- createPaymentActionStore,
12283
12215
  runClientCapability,
12284
12216
  runClientCapabilityResult,
12285
12217
  runClientCapabilityCleanup,
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  EModelProvider
3
- } from "./chunk-YSRA6KFZ.js";
3
+ } from "./chunk-RHFHCJYP.js";
4
4
 
5
5
  // ../linkque-agent-appwrite-integrate/dist/esm/runtime/protocol.js
6
6
  var baked;