@qoder-ai/qmind-cli 4.1.0 → 4.2.0
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/CHANGELOG.md +6 -0
- package/CHANGELOG.zh-CN.md +6 -0
- package/README.md +16 -1
- package/README.zh-CN.md +16 -1
- package/dist/qmind.js +702 -137
- package/package.json +5 -5
package/dist/qmind.js
CHANGED
|
@@ -389,6 +389,19 @@ function createAgentOperations(context) {
|
|
|
389
389
|
schema: retrieveResultSchema
|
|
390
390
|
});
|
|
391
391
|
},
|
|
392
|
+
async retrieveFeatured(notebookId, input, options) {
|
|
393
|
+
const notebook = requiredString$2(notebookId, "notebookId");
|
|
394
|
+
return call(context, {
|
|
395
|
+
body: retrieveBody(input),
|
|
396
|
+
callOptions: options,
|
|
397
|
+
capability: "retrieval",
|
|
398
|
+
idempotent: false,
|
|
399
|
+
method: "POST",
|
|
400
|
+
operation: "retrieval.retrieveFeatured",
|
|
401
|
+
path: context.profile.apiPath("featured-notebooks", notebook, "retrieve"),
|
|
402
|
+
schema: retrieveResultSchema
|
|
403
|
+
});
|
|
404
|
+
},
|
|
392
405
|
startAgentRun: (notebookId, input, options) => startAgentRunFor(notebookId, input, options, false),
|
|
393
406
|
startFeaturedAgentRun: (notebookId, input, options) => startAgentRunFor(notebookId, input, options, true),
|
|
394
407
|
streamFeaturedQuestion,
|
|
@@ -1763,9 +1776,31 @@ function createQMindClient(options) {
|
|
|
1763
1776
|
});
|
|
1764
1777
|
}
|
|
1765
1778
|
//#endregion
|
|
1779
|
+
//#region ../sdk/src/source-title-policy.ts
|
|
1780
|
+
const PATH_SEPARATORS = /[/\\\0]/u;
|
|
1781
|
+
/** A Source title doubles as its last path segment; this is the local shape check, not a server limit. */
|
|
1782
|
+
function qmindSourceTitlePolicy(value) {
|
|
1783
|
+
const title = value.trim();
|
|
1784
|
+
if (!title) return {
|
|
1785
|
+
allowed: false,
|
|
1786
|
+
reason: "empty"
|
|
1787
|
+
};
|
|
1788
|
+
if (title === "." || title === ".." || PATH_SEPARATORS.test(title)) return {
|
|
1789
|
+
allowed: false,
|
|
1790
|
+
reason: "notSingleSegment"
|
|
1791
|
+
};
|
|
1792
|
+
return {
|
|
1793
|
+
allowed: true,
|
|
1794
|
+
title
|
|
1795
|
+
};
|
|
1796
|
+
}
|
|
1797
|
+
//#endregion
|
|
1766
1798
|
//#region ../node-host/src/auth.ts
|
|
1767
1799
|
const responseRecordSchema = z.record(z.string(), z.unknown());
|
|
1768
|
-
const personalTokenExchangeSchema = z.object({
|
|
1800
|
+
const personalTokenExchangeSchema = z.object({
|
|
1801
|
+
token: z.string().min(1),
|
|
1802
|
+
expires_at: z.string().refine((value) => value.length === 0 || Number.isFinite(Date.parse(value))).default("")
|
|
1803
|
+
});
|
|
1769
1804
|
const devicePollSchema = z.object({
|
|
1770
1805
|
expires_at: z.string().default(""),
|
|
1771
1806
|
refresh_token: z.string().default(""),
|
|
@@ -1825,7 +1860,7 @@ function authResponseError(response, operation) {
|
|
|
1825
1860
|
...upstreamCode === void 0 ? {} : { upstreamCode }
|
|
1826
1861
|
});
|
|
1827
1862
|
}
|
|
1828
|
-
async function authRequest(transport, method, path, operation, body) {
|
|
1863
|
+
async function authRequest(transport, method, path, operation, body, signal) {
|
|
1829
1864
|
const request = {
|
|
1830
1865
|
...body === void 0 ? {} : { body: {
|
|
1831
1866
|
kind: "json",
|
|
@@ -1836,15 +1871,20 @@ async function authRequest(transport, method, path, operation, body) {
|
|
|
1836
1871
|
path
|
|
1837
1872
|
},
|
|
1838
1873
|
idempotent: method === "GET",
|
|
1839
|
-
method
|
|
1874
|
+
method,
|
|
1875
|
+
...signal === void 0 ? {} : { signal }
|
|
1840
1876
|
};
|
|
1841
1877
|
let response;
|
|
1842
1878
|
try {
|
|
1843
1879
|
response = await transport.request(request);
|
|
1844
1880
|
} catch (cause) {
|
|
1881
|
+
const abortError = aborted(signal);
|
|
1882
|
+
if (abortError !== void 0) throw abortError;
|
|
1845
1883
|
if (cause instanceof QMindError) throw cause;
|
|
1846
1884
|
throw new QMindError("NETWORK_ERROR", `${operation} failed`);
|
|
1847
1885
|
}
|
|
1886
|
+
const abortError = aborted(signal);
|
|
1887
|
+
if (abortError !== void 0) throw abortError;
|
|
1848
1888
|
if (response.status < 200 || response.status >= 300) throw authResponseError(response, operation);
|
|
1849
1889
|
const parsed = responseRecordSchema.safeParse(response.body);
|
|
1850
1890
|
if (!parsed.success) throw new QMindError("PROTOCOL_ERROR", `${operation} returned an invalid response`, {
|
|
@@ -1899,9 +1939,70 @@ function createQMindCliAuthManager(options) {
|
|
|
1899
1939
|
let credentials = options.credentials;
|
|
1900
1940
|
let explicitToken = options.explicitToken;
|
|
1901
1941
|
let token = "";
|
|
1942
|
+
let tokenExpiresAt = "";
|
|
1902
1943
|
let refreshPromise;
|
|
1944
|
+
let sessionGeneration = 0;
|
|
1945
|
+
let credentialWrite = Promise.resolve();
|
|
1946
|
+
const assertCurrentSession = (generation, operation = "refresh") => {
|
|
1947
|
+
if (generation !== sessionGeneration) throw new QMindError("AUTH_REQUIRED", `credentials changed during ${operation}`);
|
|
1948
|
+
};
|
|
1949
|
+
const serializeCredentialWrite = (operation) => {
|
|
1950
|
+
const pending = credentialWrite.then(operation);
|
|
1951
|
+
credentialWrite = pending.then(() => void 0, () => void 0);
|
|
1952
|
+
return pending;
|
|
1953
|
+
};
|
|
1954
|
+
const commitLogin = async (saved, generation, signal) => await serializeCredentialWrite(async () => {
|
|
1955
|
+
assertCurrentSession(generation, "login");
|
|
1956
|
+
const abortError = aborted(signal);
|
|
1957
|
+
if (abortError !== void 0) throw abortError;
|
|
1958
|
+
await options.vault.save(saved);
|
|
1959
|
+
if (generation !== sessionGeneration) {
|
|
1960
|
+
await options.vault.deleteIf(saved);
|
|
1961
|
+
assertCurrentSession(generation, "login");
|
|
1962
|
+
}
|
|
1963
|
+
sessionGeneration++;
|
|
1964
|
+
refreshPromise = void 0;
|
|
1965
|
+
credentials = saved;
|
|
1966
|
+
explicitToken = "";
|
|
1967
|
+
token = saved.deviceToken;
|
|
1968
|
+
tokenExpiresAt = saved.personalToken === void 0 ? "" : saved.expiresAt;
|
|
1969
|
+
return {
|
|
1970
|
+
expiresAt: saved.expiresAt,
|
|
1971
|
+
sashOrigin: saved.sashOrigin
|
|
1972
|
+
};
|
|
1973
|
+
});
|
|
1974
|
+
const exchangePersonalToken = async (personalToken, signal) => {
|
|
1975
|
+
const raw = await authRequest(options.transport, "POST", "/api/v1/jobToken/exchange", "personal token exchange", { personal_token: personalToken }, signal);
|
|
1976
|
+
const parsed = personalTokenExchangeSchema.safeParse(raw);
|
|
1977
|
+
if (!parsed.success) throw new QMindError("PROTOCOL_ERROR", "personal token exchange returned an invalid response");
|
|
1978
|
+
return parsed.data;
|
|
1979
|
+
};
|
|
1903
1980
|
const login = async (loginOptions = {}) => {
|
|
1904
|
-
|
|
1981
|
+
const personalToken = loginOptions.personalToken?.trim() ?? (explicitToken.startsWith("pt-") ? explicitToken : void 0);
|
|
1982
|
+
if (personalToken !== void 0) {
|
|
1983
|
+
if (!personalToken.startsWith("pt-") || personalToken.length <= 3 || /\s/.test(personalToken)) throw new QMindError("INVALID_ARGUMENT", "Qoder PAT must be a non-empty pt- token");
|
|
1984
|
+
if (loginOptions.clientId !== void 0) throw new QMindError("INVALID_ARGUMENT", "client_id cannot be combined with PAT login");
|
|
1985
|
+
const abortError = aborted(loginOptions.signal);
|
|
1986
|
+
if (abortError !== void 0) throw abortError;
|
|
1987
|
+
const generation = ++sessionGeneration;
|
|
1988
|
+
refreshPromise = void 0;
|
|
1989
|
+
const exchanged = await exchangePersonalToken(personalToken, loginOptions.signal);
|
|
1990
|
+
assertCurrentSession(generation, "login");
|
|
1991
|
+
const saved = {
|
|
1992
|
+
deviceToken: exchanged.token,
|
|
1993
|
+
personalToken,
|
|
1994
|
+
expiresAt: exchanged.expires_at,
|
|
1995
|
+
refreshToken: "",
|
|
1996
|
+
refreshTokenExpiresAt: "",
|
|
1997
|
+
sashOrigin: options.config.sashOrigin,
|
|
1998
|
+
savedAt: "",
|
|
1999
|
+
schemaVersion: 2
|
|
2000
|
+
};
|
|
2001
|
+
return await commitLogin(saved, generation, loginOptions.signal);
|
|
2002
|
+
}
|
|
2003
|
+
if (options.config.nonInteractive) throw new QMindError("AUTH_REQUIRED", "browser login requires an interactive terminal; use PAT login with --pat-stdin or a Qoder PAT environment variable");
|
|
2004
|
+
const generation = ++sessionGeneration;
|
|
2005
|
+
refreshPromise = void 0;
|
|
1905
2006
|
const pkce = createPkce();
|
|
1906
2007
|
const browserUrl = new URL("/device/selectAccounts", options.config.dashboardOrigin);
|
|
1907
2008
|
browserUrl.searchParams.set("nonce", pkce.nonce);
|
|
@@ -1930,14 +2031,7 @@ function createQMindCliAuthManager(options) {
|
|
|
1930
2031
|
const raw = await authRequest(options.transport, "GET", pollPath, "device authorization polling");
|
|
1931
2032
|
const parsed = devicePollSchema.safeParse(raw);
|
|
1932
2033
|
if (!parsed.success) throw new QMindError("PROTOCOL_ERROR", "device authorization returned an invalid response");
|
|
1933
|
-
|
|
1934
|
-
await options.vault.save(credentials);
|
|
1935
|
-
explicitToken = "";
|
|
1936
|
-
token = credentials.deviceToken;
|
|
1937
|
-
return {
|
|
1938
|
-
expiresAt: credentials.expiresAt,
|
|
1939
|
-
sashOrigin: credentials.sashOrigin
|
|
1940
|
-
};
|
|
2034
|
+
return await commitLogin(credentialsFromLogin(parsed.data, options.config.sashOrigin), generation, loginOptions.signal);
|
|
1941
2035
|
} catch (error) {
|
|
1942
2036
|
if (error instanceof QMindError && error.status === 404) continue;
|
|
1943
2037
|
throw error;
|
|
@@ -1946,20 +2040,55 @@ function createQMindCliAuthManager(options) {
|
|
|
1946
2040
|
throw new QMindError("AUTH_REQUIRED", "authorization timed out after 5m");
|
|
1947
2041
|
};
|
|
1948
2042
|
const refreshInternal = async (reason) => {
|
|
2043
|
+
const generation = sessionGeneration;
|
|
2044
|
+
const sourceToken = explicitToken || credentials?.deviceToken || "";
|
|
2045
|
+
const personalToken = sourceToken.startsWith("pt-") ? sourceToken : explicitToken.length === 0 ? credentials?.personalToken : void 0;
|
|
2046
|
+
if (personalToken !== void 0) {
|
|
2047
|
+
const basis = credentials;
|
|
2048
|
+
const explicitBasis = explicitToken;
|
|
2049
|
+
const exchanged = await exchangePersonalToken(personalToken);
|
|
2050
|
+
assertCurrentSession(generation);
|
|
2051
|
+
if (credentials !== basis || explicitToken !== explicitBasis) throw new QMindError("AUTH_REQUIRED", "credentials changed during refresh");
|
|
2052
|
+
if (explicitToken.length === 0 && basis !== void 0) {
|
|
2053
|
+
const refreshed = {
|
|
2054
|
+
...basis,
|
|
2055
|
+
personalToken,
|
|
2056
|
+
deviceToken: exchanged.token,
|
|
2057
|
+
expiresAt: exchanged.expires_at
|
|
2058
|
+
};
|
|
2059
|
+
const committed = await options.vault.compareAndSwap(basis, refreshed);
|
|
2060
|
+
assertCurrentSession(generation);
|
|
2061
|
+
const current = committed ? refreshed : await options.vault.load();
|
|
2062
|
+
assertCurrentSession(generation);
|
|
2063
|
+
credentials = current;
|
|
2064
|
+
token = "";
|
|
2065
|
+
tokenExpiresAt = "";
|
|
2066
|
+
if (credentials === void 0) throw new QMindError("AUTH_REQUIRED", "credentials changed during refresh");
|
|
2067
|
+
token = credentials.deviceToken;
|
|
2068
|
+
tokenExpiresAt = credentials.personalToken === void 0 ? "" : credentials.expiresAt;
|
|
2069
|
+
if (token.length === 0) throw new QMindError("AUTH_REQUIRED", "credentials changed during refresh");
|
|
2070
|
+
return token;
|
|
2071
|
+
}
|
|
2072
|
+
token = exchanged.token;
|
|
2073
|
+
tokenExpiresAt = exchanged.expires_at;
|
|
2074
|
+
return token;
|
|
2075
|
+
}
|
|
1949
2076
|
if (credentials === void 0 || credentials.refreshToken.length === 0) {
|
|
1950
2077
|
token = "";
|
|
1951
2078
|
throw new QMindError("AUTH_REQUIRED", `credentials cannot be refreshed after ${reason}`);
|
|
1952
2079
|
}
|
|
1953
2080
|
if (isExpired(credentials.refreshTokenExpiresAt, options.clock.now())) {
|
|
1954
2081
|
await options.vault.deleteIf(credentials);
|
|
2082
|
+
assertCurrentSession(generation);
|
|
1955
2083
|
credentials = void 0;
|
|
1956
2084
|
token = "";
|
|
1957
2085
|
throw new QMindError("AUTH_REQUIRED", "stored refresh credentials have expired");
|
|
1958
2086
|
}
|
|
1959
|
-
const
|
|
2087
|
+
const basis = credentials;
|
|
2088
|
+
const raw = await authRequest(options.transport, "POST", "/api/v1/deviceToken/refresh", "credential refresh", { refresh_token: basis.refreshToken });
|
|
2089
|
+
assertCurrentSession(generation);
|
|
1960
2090
|
const parsed = refreshSchema.safeParse(raw);
|
|
1961
2091
|
if (!parsed.success) throw new QMindError("PROTOCOL_ERROR", "credential refresh returned an invalid response");
|
|
1962
|
-
const basis = credentials;
|
|
1963
2092
|
const legacy = basis.schemaVersion !== 2;
|
|
1964
2093
|
const refreshed = {
|
|
1965
2094
|
...basis,
|
|
@@ -1970,7 +2099,10 @@ function createQMindCliAuthManager(options) {
|
|
|
1970
2099
|
sashOrigin: options.config.sashOrigin,
|
|
1971
2100
|
schemaVersion: 2
|
|
1972
2101
|
};
|
|
1973
|
-
const
|
|
2102
|
+
const committed = await options.vault.compareAndSwap(basis, refreshed, { migrateLegacy: legacy });
|
|
2103
|
+
assertCurrentSession(generation);
|
|
2104
|
+
const current = committed ? refreshed : await options.vault.load();
|
|
2105
|
+
assertCurrentSession(generation);
|
|
1974
2106
|
if (current === void 0) {
|
|
1975
2107
|
credentials = void 0;
|
|
1976
2108
|
token = "";
|
|
@@ -1982,37 +2114,40 @@ function createQMindCliAuthManager(options) {
|
|
|
1982
2114
|
};
|
|
1983
2115
|
const manager = {
|
|
1984
2116
|
async getToken() {
|
|
1985
|
-
if (token.length > 0)
|
|
2117
|
+
if (token.length > 0) {
|
|
2118
|
+
if (isExpired(tokenExpiresAt, options.clock.now(), EXPIRY_SKEW_MS)) return manager.refreshToken("expired", token);
|
|
2119
|
+
return token;
|
|
2120
|
+
}
|
|
1986
2121
|
const candidate = explicitToken || credentials?.deviceToken || "";
|
|
1987
2122
|
if (candidate.length === 0) {
|
|
1988
|
-
if (options.config.nonInteractive) throw new QMindError("AUTH_REQUIRED", `no credentials found; run '${options.loginCommand ?? "qmind login"}' in an interactive terminal`);
|
|
2123
|
+
if (options.config.nonInteractive) throw new QMindError("AUTH_REQUIRED", `no credentials found; run '${options.loginCommand ?? "qmind login"}' in an interactive terminal or use PAT login with --pat-stdin`);
|
|
1989
2124
|
await login();
|
|
1990
2125
|
return token;
|
|
1991
2126
|
}
|
|
1992
|
-
if (candidate.startsWith("pt-"))
|
|
1993
|
-
const raw = await authRequest(options.transport, "POST", "/api/v1/jobToken/exchange", "personal token exchange", { personal_token: candidate });
|
|
1994
|
-
const parsed = personalTokenExchangeSchema.safeParse(raw);
|
|
1995
|
-
if (!parsed.success) throw new QMindError("PROTOCOL_ERROR", "personal token exchange returned an invalid response");
|
|
1996
|
-
token = parsed.data.token;
|
|
1997
|
-
return token;
|
|
1998
|
-
}
|
|
2127
|
+
if (candidate.startsWith("pt-")) return manager.refreshToken("expired");
|
|
1999
2128
|
if (credentials !== void 0 && candidate === credentials.deviceToken && isExpired(credentials.expiresAt, options.clock.now(), EXPIRY_SKEW_MS)) return manager.refreshToken("expired", candidate);
|
|
2000
2129
|
token = candidate;
|
|
2130
|
+
tokenExpiresAt = credentials?.personalToken !== void 0 && explicitToken.length === 0 ? credentials.expiresAt : "";
|
|
2001
2131
|
return token;
|
|
2002
2132
|
},
|
|
2003
2133
|
login,
|
|
2004
2134
|
async logout() {
|
|
2005
|
-
|
|
2135
|
+
sessionGeneration++;
|
|
2006
2136
|
credentials = void 0;
|
|
2007
2137
|
explicitToken = "";
|
|
2008
2138
|
token = "";
|
|
2139
|
+
tokenExpiresAt = "";
|
|
2009
2140
|
refreshPromise = void 0;
|
|
2141
|
+
await serializeCredentialWrite(() => options.vault.delete());
|
|
2010
2142
|
},
|
|
2011
2143
|
async refreshToken(reason, rejectedToken) {
|
|
2012
2144
|
if (rejectedToken !== void 0 && token.length > 0 && token !== rejectedToken) return token;
|
|
2013
|
-
refreshPromise
|
|
2014
|
-
|
|
2015
|
-
|
|
2145
|
+
if (refreshPromise === void 0) {
|
|
2146
|
+
const pending = refreshInternal(reason).finally(() => {
|
|
2147
|
+
if (refreshPromise === pending) refreshPromise = void 0;
|
|
2148
|
+
});
|
|
2149
|
+
refreshPromise = pending;
|
|
2150
|
+
}
|
|
2016
2151
|
return await refreshPromise;
|
|
2017
2152
|
}
|
|
2018
2153
|
};
|
|
@@ -2282,6 +2417,7 @@ async function loadQMindCliConfig(options = {}) {
|
|
|
2282
2417
|
//#region ../node-host/src/credentials.ts
|
|
2283
2418
|
const serializedCredentialsSchema = z.object({
|
|
2284
2419
|
device_token: z.string().default(""),
|
|
2420
|
+
personal_token: z.string().min(4).startsWith("pt-").regex(/^\S+$/).optional(),
|
|
2285
2421
|
expires_at: z.string().default(""),
|
|
2286
2422
|
refresh_token: z.string().default(""),
|
|
2287
2423
|
refresh_token_expires_at: z.string().default(""),
|
|
@@ -2308,6 +2444,7 @@ function normalizeStoredCredentials(value) {
|
|
|
2308
2444
|
if (parsed.schema_version !== void 0 && parsed.schema_version > 2 || parsed.device_token.length === 0 && parsed.refresh_token.length === 0 || !isValidOptionalTimestamp(parsed.expires_at) || !isValidOptionalTimestamp(parsed.refresh_token_expires_at)) throw storedCredentialsInvalid();
|
|
2309
2445
|
return {
|
|
2310
2446
|
deviceToken: parsed.device_token,
|
|
2447
|
+
...parsed.personal_token === void 0 ? {} : { personalToken: parsed.personal_token },
|
|
2311
2448
|
expiresAt: parsed.expires_at,
|
|
2312
2449
|
refreshToken: parsed.refresh_token,
|
|
2313
2450
|
refreshTokenExpiresAt: parsed.refresh_token_expires_at,
|
|
@@ -2319,6 +2456,7 @@ function normalizeStoredCredentials(value) {
|
|
|
2319
2456
|
function serializedCredentials(credentials, now) {
|
|
2320
2457
|
return `${JSON.stringify({
|
|
2321
2458
|
device_token: credentials.deviceToken,
|
|
2459
|
+
...credentials.personalToken === void 0 ? {} : { personal_token: credentials.personalToken },
|
|
2322
2460
|
expires_at: credentials.expiresAt,
|
|
2323
2461
|
refresh_token: credentials.refreshToken,
|
|
2324
2462
|
refresh_token_expires_at: credentials.refreshTokenExpiresAt,
|
|
@@ -2328,7 +2466,7 @@ function serializedCredentials(credentials, now) {
|
|
|
2328
2466
|
}, null, 2)}\n`;
|
|
2329
2467
|
}
|
|
2330
2468
|
function sameCredentials(left, right) {
|
|
2331
|
-
return left !== void 0 && right !== void 0 && left.deviceToken === right.deviceToken && left.expiresAt === right.expiresAt && left.refreshToken === right.refreshToken && left.refreshTokenExpiresAt === right.refreshTokenExpiresAt && left.sashOrigin === right.sashOrigin && left.schemaVersion === right.schemaVersion;
|
|
2469
|
+
return left !== void 0 && right !== void 0 && left.deviceToken === right.deviceToken && left.personalToken === right.personalToken && left.expiresAt === right.expiresAt && left.refreshToken === right.refreshToken && left.refreshTokenExpiresAt === right.refreshTokenExpiresAt && left.sashOrigin === right.sashOrigin && left.schemaVersion === right.schemaVersion;
|
|
2332
2470
|
}
|
|
2333
2471
|
async function secureDirectory(path) {
|
|
2334
2472
|
await mkdir(path, {
|
|
@@ -3363,8 +3501,8 @@ function requestsNdjsonOutput(argv) {
|
|
|
3363
3501
|
//#region src/build-info.ts
|
|
3364
3502
|
/** Bundled identity, never inferred from argv, environment, or an endpoint. */
|
|
3365
3503
|
const QMIND_CLI_DISTRIBUTION = "global";
|
|
3366
|
-
const QMIND_CLI_VERSION = "4.
|
|
3367
|
-
const QMIND_CLI_BUILD_TIME = "2026-10-
|
|
3504
|
+
const QMIND_CLI_VERSION = "4.2.0";
|
|
3505
|
+
const QMIND_CLI_BUILD_TIME = "2026-10-27T06:23:11.000Z";
|
|
3368
3506
|
function qmindCliPlatform() {
|
|
3369
3507
|
return process.platform === "win32" ? "windows" : process.platform;
|
|
3370
3508
|
}
|
|
@@ -4594,10 +4732,23 @@ function cliHostConfiguration(distribution, flags, environment, stateRoot) {
|
|
|
4594
4732
|
sashOrigin: normalizeQMindOrigin(String(flags.sash).trim(), "--sash")
|
|
4595
4733
|
} : resolveQMindProductEndpoints(productId, preset);
|
|
4596
4734
|
const target = custom ? `custom-${createHash("sha256").update(JSON.stringify([endpoints.dashboardOrigin, endpoints.sashOrigin])).digest("hex")}` : preset;
|
|
4735
|
+
const modeToken = (target === "prod" ? environment.QMIND_TOKEN : environment.QMIND_DEBUG_TOKEN)?.trim();
|
|
4736
|
+
const patVariable = !custom && (preset === "prod" || preset === "test") ? {
|
|
4737
|
+
global: {
|
|
4738
|
+
prod: "QODER_PROD_PAT",
|
|
4739
|
+
test: "QODER_TEST_PAT"
|
|
4740
|
+
},
|
|
4741
|
+
cn: {
|
|
4742
|
+
prod: "QODER_PROD_CN_PAT",
|
|
4743
|
+
test: "QODER_TEST_CN_PAT"
|
|
4744
|
+
}
|
|
4745
|
+
}[distribution][preset] : void 0;
|
|
4746
|
+
const personalToken = custom ? void 0 : (patVariable === void 0 ? void 0 : environment[patVariable]?.trim()) || environment.QODER_PERSONAL_ACCESS_TOKEN?.trim();
|
|
4747
|
+
if (!flags.token?.trim() && !modeToken && personalToken && (!personalToken.startsWith("pt-") || personalToken.length <= 3 || /\s/.test(personalToken))) throw new QMindError("INVALID_ARGUMENT", "Qoder PAT environment variable must contain a non-empty pt- token");
|
|
4597
4748
|
return {
|
|
4598
4749
|
environment: {
|
|
4599
4750
|
...environment,
|
|
4600
|
-
QMIND_TOKEN:
|
|
4751
|
+
QMIND_TOKEN: modeToken || personalToken
|
|
4601
4752
|
},
|
|
4602
4753
|
flags,
|
|
4603
4754
|
fixedEndpoints: endpoints,
|
|
@@ -4613,6 +4764,21 @@ function processIO() {
|
|
|
4613
4764
|
return Object.freeze({
|
|
4614
4765
|
stdinIsTTY: process.stdin.isTTY === true,
|
|
4615
4766
|
stderrIsTTY: process.stderr.isTTY === true,
|
|
4767
|
+
async readStdin() {
|
|
4768
|
+
const chunks = [];
|
|
4769
|
+
let size = 0;
|
|
4770
|
+
try {
|
|
4771
|
+
for await (const chunk of process.stdin) {
|
|
4772
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
4773
|
+
size += bytes.length;
|
|
4774
|
+
if (size > 16384) throw new Error("input too large");
|
|
4775
|
+
chunks.push(bytes);
|
|
4776
|
+
}
|
|
4777
|
+
} catch {
|
|
4778
|
+
throw new QMindError("INVALID_ARGUMENT", "unable to read PAT from stdin (maximum 16 KiB)");
|
|
4779
|
+
}
|
|
4780
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
4781
|
+
},
|
|
4616
4782
|
stderr(value) {
|
|
4617
4783
|
process.stderr.write(value);
|
|
4618
4784
|
},
|
|
@@ -4807,12 +4973,13 @@ function canonicalize(object, seen = /* @__PURE__ */ new Set()) {
|
|
|
4807
4973
|
}
|
|
4808
4974
|
//#endregion
|
|
4809
4975
|
//#region ../agent-core/src/contract.ts
|
|
4810
|
-
const CONTRACT_VERSION = "
|
|
4976
|
+
const CONTRACT_VERSION = "6.0.0";
|
|
4811
4977
|
const AGENT_ERROR_CODES = Object.freeze([
|
|
4812
4978
|
"invalid_input",
|
|
4813
4979
|
"selection_required",
|
|
4814
4980
|
"not_found",
|
|
4815
4981
|
"already_exists",
|
|
4982
|
+
"source_conflict",
|
|
4816
4983
|
"quota_exceeded",
|
|
4817
4984
|
"capability_unavailable",
|
|
4818
4985
|
"source_not_ready",
|
|
@@ -4851,6 +5018,7 @@ const listNotebooksInputSchema = z.object({
|
|
|
4851
5018
|
relationship: z.enum([
|
|
4852
5019
|
"owned",
|
|
4853
5020
|
"shared",
|
|
5021
|
+
"featured",
|
|
4854
5022
|
"all"
|
|
4855
5023
|
]).optional(),
|
|
4856
5024
|
cursor: z.string().min(1).max(512).optional()
|
|
@@ -4883,27 +5051,37 @@ const readSourceInputSchema = z.object({
|
|
|
4883
5051
|
offset: z.number().int().min(0).default(0),
|
|
4884
5052
|
sourceId: opaqueId
|
|
4885
5053
|
}).strict();
|
|
4886
|
-
const sourceInputSchema = z.discriminatedUnion("kind", [
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
|
|
4890
|
-
|
|
4891
|
-
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
|
|
4896
|
-
|
|
4897
|
-
|
|
5054
|
+
const sourceInputSchema = z.discriminatedUnion("kind", [
|
|
5055
|
+
z.object({
|
|
5056
|
+
kind: z.literal("text"),
|
|
5057
|
+
content: z.string().min(1).max(2e6).refine((value) => value.trim().length > 0),
|
|
5058
|
+
title: z.string().trim().min(1).max(500)
|
|
5059
|
+
}).strict(),
|
|
5060
|
+
z.object({
|
|
5061
|
+
kind: z.literal("url"),
|
|
5062
|
+
title: z.string().trim().min(1).max(500).optional(),
|
|
5063
|
+
url: z.string().trim().url().max(4096).refine((value) => {
|
|
5064
|
+
const url = new URL(value);
|
|
5065
|
+
return url.protocol === "https:" && url.username.length === 0 && url.password.length === 0;
|
|
5066
|
+
}, { message: "url must use HTTPS without embedded credentials" })
|
|
5067
|
+
}).strict(),
|
|
5068
|
+
z.object({
|
|
5069
|
+
filePath: z.string().trim().min(1).max(4096),
|
|
5070
|
+
kind: z.literal("file"),
|
|
5071
|
+
title: z.string().trim().min(1).max(500).optional()
|
|
5072
|
+
}).strict()
|
|
5073
|
+
]);
|
|
4898
5074
|
const addSourceInputSchema = z.object({
|
|
4899
5075
|
notebookId: opaqueId,
|
|
4900
|
-
source: sourceInputSchema
|
|
4901
|
-
|
|
5076
|
+
source: sourceInputSchema,
|
|
5077
|
+
parentSourceId: opaqueId.optional(),
|
|
5078
|
+
path: z.string().trim().min(1).max(4096).optional()
|
|
5079
|
+
}).strict().refine((value) => !value.parentSourceId || !!value.path, { message: "parentSourceId requires a logical path" });
|
|
4902
5080
|
const deleteSourceInputSchema = z.object({
|
|
4903
5081
|
notebookId: opaqueId,
|
|
4904
5082
|
sourceIds: z.array(opaqueId).min(1).max(100).refine((ids) => new Set(ids).size === ids.length)
|
|
4905
5083
|
}).strict();
|
|
4906
|
-
const directoryName = z.string().trim().min(1).max(500).refine((value) =>
|
|
5084
|
+
const directoryName = z.string().trim().min(1).max(500).refine((value) => qmindSourceTitlePolicy(value).allowed, { message: "title must be one path segment" });
|
|
4907
5085
|
const createDirectoryInputSchema = z.object({
|
|
4908
5086
|
notebookId: opaqueId,
|
|
4909
5087
|
title: directoryName,
|
|
@@ -4915,6 +5093,21 @@ const moveSourceInputSchema = z.object({
|
|
|
4915
5093
|
targetParentSourceId: opaqueId.nullable()
|
|
4916
5094
|
}).strict();
|
|
4917
5095
|
const agentToolInputSchemas = Object.freeze({
|
|
5096
|
+
"qmind.update_source_content": z.object({
|
|
5097
|
+
notebookId: opaqueId,
|
|
5098
|
+
sourceId: opaqueId,
|
|
5099
|
+
content: z.string().min(1).max(2e6).refine((value) => value.trim().length > 0),
|
|
5100
|
+
title: z.string().trim().min(1).max(500).optional(),
|
|
5101
|
+
baseUpdatedAt: z.string().trim().min(1).max(128),
|
|
5102
|
+
baseContentHash: z.string().regex(/^[a-f0-9]{64}$/).optional()
|
|
5103
|
+
}).strict(),
|
|
5104
|
+
"qmind.refresh_source": getSourceInputSchema,
|
|
5105
|
+
"qmind.list_task_runs": z.object({
|
|
5106
|
+
notebookId: opaqueId,
|
|
5107
|
+
sourceId: opaqueId.optional(),
|
|
5108
|
+
page,
|
|
5109
|
+
pageSize
|
|
5110
|
+
}).strict(),
|
|
4918
5111
|
"qmind.create_notebook": createNotebookInputSchema,
|
|
4919
5112
|
"qmind.delete_notebook": deleteNotebookInputSchema,
|
|
4920
5113
|
"qmind.create_scheduled_task": createScheduledTaskInputSchema,
|
|
@@ -4932,6 +5125,28 @@ const agentToolInputSchemas = Object.freeze({
|
|
|
4932
5125
|
"qmind.retrieve": retrieveInputSchema
|
|
4933
5126
|
});
|
|
4934
5127
|
const agentToolDefinitions = Object.freeze([
|
|
5128
|
+
{
|
|
5129
|
+
id: "qmind.update_source_content",
|
|
5130
|
+
leaf: "update_source_content",
|
|
5131
|
+
intent: "write",
|
|
5132
|
+
destructive: true,
|
|
5133
|
+
deadlineMs: 12e4,
|
|
5134
|
+
description: "Update source text using baseUpdatedAt from a fresh read and optionally a SHA-256 content snapshot. This preflight is not atomic compare-and-swap. Then request source refresh. Partial success means the content was saved; never rewrite it to retry refresh. Reconcile outcome_unknown before any new write."
|
|
5135
|
+
},
|
|
5136
|
+
{
|
|
5137
|
+
id: "qmind.refresh_source",
|
|
5138
|
+
leaf: "refresh_source",
|
|
5139
|
+
intent: "write",
|
|
5140
|
+
deadlineMs: 3e4,
|
|
5141
|
+
description: "Request a source_refresh task for an existing source. Success acknowledges task creation, not completed ingestion. Observe list_task_runs. Never replay an unknown task creation automatically."
|
|
5142
|
+
},
|
|
5143
|
+
{
|
|
5144
|
+
id: "qmind.list_task_runs",
|
|
5145
|
+
leaf: "list_task_runs",
|
|
5146
|
+
intent: "read",
|
|
5147
|
+
deadlineMs: 3e4,
|
|
5148
|
+
description: "List remote QMind task executions, optionally for one source. These are not host edit sessions or Agent operation IDs. Paginated results report observed upstream state, not a new execution."
|
|
5149
|
+
},
|
|
4935
5150
|
{
|
|
4936
5151
|
deadlineMs: 3e4,
|
|
4937
5152
|
description: "Create a QMind notebook with a title and optional description. Requires an account context without a host Notebook scope or policy; creation never binds the new notebook to a workspace or expands scope. Duplicate titles create independent notebooks. Never retry outcome_unknown automatically; inspect current notebooks first, and do not treat a matching title as proof of creation.",
|
|
@@ -4956,7 +5171,7 @@ const agentToolDefinitions = Object.freeze([
|
|
|
4956
5171
|
},
|
|
4957
5172
|
{
|
|
4958
5173
|
deadlineMs: 3e4,
|
|
4959
|
-
description: "Discover QMind notebooks using summary/topics and relationship (owned/shared). The host can default to owned plus shared; otherwise the default is owned. Discovery stays within the current chat scope and server-filtered retrieval scope. Discovery returns hasMore and nextCursor; continue with cursor and the same relationship, even after an empty page. Results are dynamic, ordered owned then shared, without an exact total. Use the question and overviews to select at most 5 relevant notebooks; never blindly retrieve every notebook. Legacy account listing without relationship or cursor retains page/pageSize and owned-only behavior.",
|
|
5174
|
+
description: "Discover QMind notebooks using summary/topics and relationship (owned/shared/featured). The host can default to owned plus shared plus Featured; otherwise the default is owned. Discovery stays within the current chat scope and server-filtered retrieval scope. Discovery returns hasMore and nextCursor; continue with cursor and the same relationship, even after an empty page. Results are dynamic, ordered owned then shared then Featured, without an exact total. Use the question and overviews to select at most 5 relevant notebooks; never blindly retrieve every notebook. Legacy account listing without relationship or cursor retains page/pageSize and owned-only behavior.",
|
|
4960
5175
|
id: "qmind.list_notebooks",
|
|
4961
5176
|
intent: "read",
|
|
4962
5177
|
leaf: "list_notebooks"
|
|
@@ -4984,7 +5199,7 @@ const agentToolDefinitions = Object.freeze([
|
|
|
4984
5199
|
},
|
|
4985
5200
|
{
|
|
4986
5201
|
deadlineMs: 12e4,
|
|
4987
|
-
description: "Add an HTTPS URL
|
|
5202
|
+
description: "Add an HTTPS URL, authorized local file, or UTF-8 Markdown text to QMind. Text requires title and content; filePath must be readable by the host. parentSourceId requires a logical path. Success means source creation, not completed ingestion. Never replay outcome_unknown automatically.",
|
|
4988
5203
|
id: "qmind.add_source",
|
|
4989
5204
|
intent: "write",
|
|
4990
5205
|
leaf: "add_source"
|
|
@@ -5055,6 +5270,15 @@ function deepFreeze(value) {
|
|
|
5055
5270
|
const canonicalDescriptor = canonicalize(deepFreeze({
|
|
5056
5271
|
contractVersion: CONTRACT_VERSION,
|
|
5057
5272
|
definitions: {
|
|
5273
|
+
TaskRun: {
|
|
5274
|
+
id: "opaqueId",
|
|
5275
|
+
notebookId: "opaqueId",
|
|
5276
|
+
sourceId: "opaqueId?",
|
|
5277
|
+
sceneType: "string",
|
|
5278
|
+
status: "pending|running|completed|failed|cancelled|unknown",
|
|
5279
|
+
createdAt: "iso8601?",
|
|
5280
|
+
finishedAt: "iso8601?"
|
|
5281
|
+
},
|
|
5058
5282
|
ScheduledTask: {
|
|
5059
5283
|
id: "opaqueId",
|
|
5060
5284
|
notebookId: "opaqueId",
|
|
@@ -5097,7 +5321,8 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5097
5321
|
} } : {}
|
|
5098
5322
|
})),
|
|
5099
5323
|
invocation: {
|
|
5100
|
-
|
|
5324
|
+
defaultRetrievalMode: "all?; explicit host opt-in searches each authorized target with concurrency at most 3 and bounded partial results",
|
|
5325
|
+
defaultNotebookRelationship: "owned|shared|featured|all?; host discovery default independent of notebookPolicy; omitted means owned",
|
|
5101
5326
|
approvedDeletionTargets: "SourceDeletionTarget[1..100]?; host-only, must match fresh targets",
|
|
5102
5327
|
notebookPolicy: { selectNotebook: "host callback: excluded|allowed|preferred; request-local; intersects server ACL" },
|
|
5103
5328
|
notebookScope: {
|
|
@@ -5160,6 +5385,70 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5160
5385
|
]
|
|
5161
5386
|
},
|
|
5162
5387
|
tools: [
|
|
5388
|
+
{
|
|
5389
|
+
id: "qmind.update_source_content",
|
|
5390
|
+
intent: "write",
|
|
5391
|
+
timeoutMs: 12e4,
|
|
5392
|
+
annotations: {
|
|
5393
|
+
destructiveHint: true,
|
|
5394
|
+
idempotentHint: false,
|
|
5395
|
+
openWorldHint: true,
|
|
5396
|
+
readOnlyHint: false
|
|
5397
|
+
},
|
|
5398
|
+
input: {
|
|
5399
|
+
notebookId: "opaqueId",
|
|
5400
|
+
sourceId: "opaqueId",
|
|
5401
|
+
content: "nonblank-string:1..2000000",
|
|
5402
|
+
title: "string:1..500?",
|
|
5403
|
+
baseUpdatedAt: "string:1..128",
|
|
5404
|
+
baseContentHash: "sha256?"
|
|
5405
|
+
},
|
|
5406
|
+
output: {
|
|
5407
|
+
sourceId: "opaqueId",
|
|
5408
|
+
contentUpdated: true,
|
|
5409
|
+
refresh: "accepted|failed|outcome_unknown",
|
|
5410
|
+
taskRun: "TaskRun?"
|
|
5411
|
+
}
|
|
5412
|
+
},
|
|
5413
|
+
{
|
|
5414
|
+
id: "qmind.refresh_source",
|
|
5415
|
+
intent: "write",
|
|
5416
|
+
timeoutMs: 3e4,
|
|
5417
|
+
annotations: {
|
|
5418
|
+
destructiveHint: false,
|
|
5419
|
+
idempotentHint: false,
|
|
5420
|
+
openWorldHint: true,
|
|
5421
|
+
readOnlyHint: false
|
|
5422
|
+
},
|
|
5423
|
+
input: {
|
|
5424
|
+
notebookId: "opaqueId",
|
|
5425
|
+
sourceId: "opaqueId"
|
|
5426
|
+
},
|
|
5427
|
+
output: "TaskRun"
|
|
5428
|
+
},
|
|
5429
|
+
{
|
|
5430
|
+
id: "qmind.list_task_runs",
|
|
5431
|
+
intent: "read",
|
|
5432
|
+
timeoutMs: 3e4,
|
|
5433
|
+
annotations: {
|
|
5434
|
+
destructiveHint: false,
|
|
5435
|
+
idempotentHint: true,
|
|
5436
|
+
openWorldHint: true,
|
|
5437
|
+
readOnlyHint: true
|
|
5438
|
+
},
|
|
5439
|
+
input: {
|
|
5440
|
+
notebookId: "opaqueId",
|
|
5441
|
+
sourceId: "opaqueId?",
|
|
5442
|
+
page: "integer:1..?",
|
|
5443
|
+
pageSize: "integer:1..50?"
|
|
5444
|
+
},
|
|
5445
|
+
output: {
|
|
5446
|
+
runs: ["TaskRun"],
|
|
5447
|
+
totalSize: "integer:0..",
|
|
5448
|
+
page: "integer:1..",
|
|
5449
|
+
pageSize: "integer:1..50"
|
|
5450
|
+
}
|
|
5451
|
+
},
|
|
5163
5452
|
{
|
|
5164
5453
|
annotations: {
|
|
5165
5454
|
destructiveHint: false,
|
|
@@ -5227,7 +5516,9 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5227
5516
|
score: "number",
|
|
5228
5517
|
sourceId: "opaqueId",
|
|
5229
5518
|
sourceTitle: "string",
|
|
5230
|
-
tokenCount: "integer:0.."
|
|
5519
|
+
tokenCount: "integer:0..",
|
|
5520
|
+
lineStart: "positiveInteger?; validated chunk or matching citation metadata",
|
|
5521
|
+
lineEnd: "positiveInteger?; at least lineStart"
|
|
5231
5522
|
}],
|
|
5232
5523
|
promptContext: "string",
|
|
5233
5524
|
reranked: "boolean",
|
|
@@ -5251,7 +5542,7 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5251
5542
|
input: {
|
|
5252
5543
|
page: "integer:1..?",
|
|
5253
5544
|
pageSize: "integer:1..50?",
|
|
5254
|
-
relationship: "owned|shared|all?",
|
|
5545
|
+
relationship: "owned|shared|featured|all?",
|
|
5255
5546
|
cursor: "opaqueString?; continuation for filtered discovery"
|
|
5256
5547
|
},
|
|
5257
5548
|
intent: "read",
|
|
@@ -5263,7 +5554,7 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5263
5554
|
sourceCount: "integer",
|
|
5264
5555
|
status: "string",
|
|
5265
5556
|
preferred: "boolean",
|
|
5266
|
-
relationship: "owned|shared?",
|
|
5557
|
+
relationship: "owned|shared|featured?",
|
|
5267
5558
|
summary: "string?",
|
|
5268
5559
|
title: "string",
|
|
5269
5560
|
topics: "string[]?",
|
|
@@ -5359,21 +5650,30 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5359
5650
|
id: "qmind.add_source",
|
|
5360
5651
|
input: {
|
|
5361
5652
|
notebookId: "opaqueId",
|
|
5362
|
-
source: [
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5368
|
-
|
|
5369
|
-
|
|
5370
|
-
|
|
5653
|
+
source: [
|
|
5654
|
+
{
|
|
5655
|
+
kind: "url",
|
|
5656
|
+
title: "string:1..500?",
|
|
5657
|
+
url: "credential-free-https:1..4096"
|
|
5658
|
+
},
|
|
5659
|
+
{
|
|
5660
|
+
filePath: "string:1..4096",
|
|
5661
|
+
kind: "file",
|
|
5662
|
+
title: "string:1..500?"
|
|
5663
|
+
},
|
|
5664
|
+
{
|
|
5665
|
+
kind: "text",
|
|
5666
|
+
title: "string:1..500",
|
|
5667
|
+
content: "nonblank-string:1..2000000"
|
|
5668
|
+
}
|
|
5669
|
+
]
|
|
5371
5670
|
},
|
|
5372
5671
|
intent: "write",
|
|
5373
5672
|
output: "Source",
|
|
5374
5673
|
timeoutMs: {
|
|
5375
5674
|
file: 3e5,
|
|
5376
|
-
url: 12e4
|
|
5675
|
+
url: 12e4,
|
|
5676
|
+
text: 12e4
|
|
5377
5677
|
}
|
|
5378
5678
|
},
|
|
5379
5679
|
{
|
|
@@ -5517,13 +5817,24 @@ const canonicalDescriptor = canonicalize(deepFreeze({
|
|
|
5517
5817
|
}
|
|
5518
5818
|
}
|
|
5519
5819
|
],
|
|
5520
|
-
warningCatalog: [
|
|
5521
|
-
|
|
5522
|
-
|
|
5523
|
-
|
|
5524
|
-
|
|
5525
|
-
|
|
5526
|
-
|
|
5820
|
+
warningCatalog: [
|
|
5821
|
+
{
|
|
5822
|
+
code: "source_refresh_failed",
|
|
5823
|
+
fields: ["code", "notebookId"]
|
|
5824
|
+
},
|
|
5825
|
+
{
|
|
5826
|
+
code: "source_refresh_unknown",
|
|
5827
|
+
fields: ["code", "notebookId"]
|
|
5828
|
+
},
|
|
5829
|
+
{
|
|
5830
|
+
code: "notebook_search_failed",
|
|
5831
|
+
fields: ["code", "notebookId"]
|
|
5832
|
+
},
|
|
5833
|
+
{
|
|
5834
|
+
code: "source_deletion_failed",
|
|
5835
|
+
fields: ["code", "notebookId"]
|
|
5836
|
+
}
|
|
5837
|
+
]
|
|
5527
5838
|
}));
|
|
5528
5839
|
if (canonicalDescriptor === void 0) throw new Error("QMind Agent Contract is not serializable");
|
|
5529
5840
|
createHash("sha256").update(canonicalDescriptor, "utf8").digest("hex");
|
|
@@ -5564,6 +5875,7 @@ function errorCode(error, mutationStarted = false) {
|
|
|
5564
5875
|
* pushed a per-operation identity, its bearer token as the request authorization override.
|
|
5565
5876
|
*/
|
|
5566
5877
|
function callOptions(invocation) {
|
|
5878
|
+
invocation.assertCurrent?.();
|
|
5567
5879
|
return {
|
|
5568
5880
|
...invocation.token === void 0 ? {} : { authorization: invocation.token },
|
|
5569
5881
|
signal: invocation.signal
|
|
@@ -5571,9 +5883,15 @@ function callOptions(invocation) {
|
|
|
5571
5883
|
}
|
|
5572
5884
|
//#endregion
|
|
5573
5885
|
//#region ../agent-core/src/organizationPolicy.ts
|
|
5574
|
-
/**
|
|
5575
|
-
function
|
|
5576
|
-
if (typeof error !== "object" || error === null || !("upstreamCode" in error)
|
|
5886
|
+
/** 企业策略仅保留白名单业务含义,不暴露上游正文。 */
|
|
5887
|
+
function qmindOrganizationPolicyError(error) {
|
|
5888
|
+
if (typeof error !== "object" || error === null || !("upstreamCode" in error)) return void 0;
|
|
5889
|
+
if (error.upstreamCode === "QMindPolicyUnavailable") return {
|
|
5890
|
+
code: "upstream_unavailable",
|
|
5891
|
+
message: "知识库管控服务暂时不可用,请稍后重试。",
|
|
5892
|
+
retryable: true
|
|
5893
|
+
};
|
|
5894
|
+
if (error.upstreamCode !== "QMindDisabledByOrganization") return void 0;
|
|
5577
5895
|
return {
|
|
5578
5896
|
code: "permission_denied",
|
|
5579
5897
|
message: "企业管理员已禁用知识库功能,暂时无法使用。请联系企业管理员开启知识库功能后再试;重复调用无法恢复访问。",
|
|
@@ -5581,6 +5899,120 @@ function qmindOrganizationDisabledError(error) {
|
|
|
5581
5899
|
};
|
|
5582
5900
|
}
|
|
5583
5901
|
//#endregion
|
|
5902
|
+
//#region ../agent-core/src/source-content.ts
|
|
5903
|
+
function taskData$1(run) {
|
|
5904
|
+
return {
|
|
5905
|
+
id: run.id,
|
|
5906
|
+
notebookId: run.notebookId,
|
|
5907
|
+
sourceId: run.sourceId,
|
|
5908
|
+
sceneType: run.sceneType,
|
|
5909
|
+
status: run.status,
|
|
5910
|
+
createdAt: run.createdAt,
|
|
5911
|
+
finishedAt: run.finishedAt
|
|
5912
|
+
};
|
|
5913
|
+
}
|
|
5914
|
+
function mutationFailure(error, invocation) {
|
|
5915
|
+
const policy = qmindOrganizationPolicyError(error);
|
|
5916
|
+
if (policy) return {
|
|
5917
|
+
ok: false,
|
|
5918
|
+
error: policy,
|
|
5919
|
+
meta: { operationId: invocation.operationId }
|
|
5920
|
+
};
|
|
5921
|
+
const code = errorCode(error, true);
|
|
5922
|
+
return failure$1(invocation.operationId, code === "internal" ? "outcome_unknown" : code, "The source operation did not return a confirmed result. Reconcile before another write.");
|
|
5923
|
+
}
|
|
5924
|
+
async function refreshSource(client, input, invocation) {
|
|
5925
|
+
if (!client.createTaskRun) return failure$1(invocation.operationId, "capability_unavailable", "Source refresh is unavailable.");
|
|
5926
|
+
const options = callOptions(invocation);
|
|
5927
|
+
invocation.signal?.throwIfAborted();
|
|
5928
|
+
try {
|
|
5929
|
+
const run = await client.createTaskRun(input.notebookId, {
|
|
5930
|
+
sceneType: "source_refresh",
|
|
5931
|
+
trigger: "manual",
|
|
5932
|
+
sourceId: input.sourceId
|
|
5933
|
+
}, options);
|
|
5934
|
+
if (!run.id || run.notebookId !== input.notebookId || run.sourceId !== input.sourceId) throw new TypeError("Unconfirmed source refresh.");
|
|
5935
|
+
return {
|
|
5936
|
+
ok: true,
|
|
5937
|
+
data: taskData$1(run),
|
|
5938
|
+
meta: { operationId: invocation.operationId }
|
|
5939
|
+
};
|
|
5940
|
+
} catch (error) {
|
|
5941
|
+
return mutationFailure(error, invocation);
|
|
5942
|
+
}
|
|
5943
|
+
}
|
|
5944
|
+
async function listTaskRuns(client, input, invocation) {
|
|
5945
|
+
if (!client.listTaskRuns) return failure$1(invocation.operationId, "capability_unavailable", "Task history is unavailable.");
|
|
5946
|
+
const result = await client.listTaskRuns(input.notebookId, {
|
|
5947
|
+
page: input.page,
|
|
5948
|
+
pageSize: input.pageSize,
|
|
5949
|
+
...input.sourceId === void 0 ? {} : { sourceId: input.sourceId }
|
|
5950
|
+
}, callOptions(invocation));
|
|
5951
|
+
return {
|
|
5952
|
+
ok: true,
|
|
5953
|
+
data: {
|
|
5954
|
+
runs: result.runs.map(taskData$1),
|
|
5955
|
+
totalSize: result.totalSize,
|
|
5956
|
+
page: input.page,
|
|
5957
|
+
pageSize: input.pageSize
|
|
5958
|
+
},
|
|
5959
|
+
meta: { operationId: invocation.operationId }
|
|
5960
|
+
};
|
|
5961
|
+
}
|
|
5962
|
+
async function updateSourceContent(client, input, invocation) {
|
|
5963
|
+
if (!client.updateSource || !client.createTaskRun) return failure$1(invocation.operationId, "capability_unavailable", "Source writeback is unavailable.");
|
|
5964
|
+
const current = await client.getSource(input.notebookId, input.sourceId, callOptions(invocation));
|
|
5965
|
+
if (current.isDir) return failure$1(invocation.operationId, "invalid_input", "Directory content cannot be updated.");
|
|
5966
|
+
if (current.updatedAt !== input.baseUpdatedAt) return failure$1(invocation.operationId, "source_conflict", "Source changed; read it before editing.");
|
|
5967
|
+
if (input.baseContentHash !== void 0) {
|
|
5968
|
+
const content = await client.getSourceContent(input.notebookId, input.sourceId, callOptions(invocation));
|
|
5969
|
+
if (createHash("sha256").update(content.content).digest("hex") !== input.baseContentHash) return failure$1(invocation.operationId, "source_conflict", "Source content changed before editing.");
|
|
5970
|
+
}
|
|
5971
|
+
invocation.signal?.throwIfAborted();
|
|
5972
|
+
const options = callOptions(invocation);
|
|
5973
|
+
let updated;
|
|
5974
|
+
try {
|
|
5975
|
+
updated = await client.updateSource(input.notebookId, input.sourceId, {
|
|
5976
|
+
content: input.content,
|
|
5977
|
+
sourceType: "markdown",
|
|
5978
|
+
status: "ready",
|
|
5979
|
+
...input.title === void 0 ? {} : { title: input.title }
|
|
5980
|
+
}, options);
|
|
5981
|
+
if (updated.id !== input.sourceId || updated.notebookId !== input.notebookId) throw new TypeError("Unconfirmed source update.");
|
|
5982
|
+
} catch (error) {
|
|
5983
|
+
return mutationFailure(error, invocation);
|
|
5984
|
+
}
|
|
5985
|
+
let refresh;
|
|
5986
|
+
try {
|
|
5987
|
+
refresh = await refreshSource(client, input, invocation);
|
|
5988
|
+
} catch (error) {
|
|
5989
|
+
refresh = failure$1(invocation.operationId, errorCode(error), "Refresh was not submitted.");
|
|
5990
|
+
}
|
|
5991
|
+
const unknown = !refresh.ok && refresh.error.code === "outcome_unknown";
|
|
5992
|
+
return {
|
|
5993
|
+
ok: true,
|
|
5994
|
+
data: {
|
|
5995
|
+
notebookId: input.notebookId,
|
|
5996
|
+
sourceId: updated.id,
|
|
5997
|
+
title: updated.title,
|
|
5998
|
+
updatedAt: updated.updatedAt,
|
|
5999
|
+
contentUpdated: true,
|
|
6000
|
+
refresh: refresh.ok ? "accepted" : unknown ? "outcome_unknown" : "failed",
|
|
6001
|
+
...refresh.ok ? { taskRun: refresh.data } : {}
|
|
6002
|
+
},
|
|
6003
|
+
meta: {
|
|
6004
|
+
operationId: invocation.operationId,
|
|
6005
|
+
...!refresh.ok ? {
|
|
6006
|
+
partial: true,
|
|
6007
|
+
warnings: [{
|
|
6008
|
+
code: unknown ? "source_refresh_unknown" : "source_refresh_failed",
|
|
6009
|
+
notebookId: input.notebookId
|
|
6010
|
+
}]
|
|
6011
|
+
} : {}
|
|
6012
|
+
}
|
|
6013
|
+
};
|
|
6014
|
+
}
|
|
6015
|
+
//#endregion
|
|
5584
6016
|
//#region ../agent-core/src/notebook-mutations.ts
|
|
5585
6017
|
async function runNotebookMutation(client, tool, rawInput, invocation) {
|
|
5586
6018
|
if (tool === "qmind.create_notebook" && !client.createNotebook || tool === "qmind.delete_notebook" && !client.deleteNotebook) return failure$1(invocation.operationId, "capability_unavailable", "This client does not support this notebook operation.");
|
|
@@ -5618,7 +6050,7 @@ async function runNotebookMutation(client, tool, rawInput, invocation) {
|
|
|
5618
6050
|
meta: { operationId: invocation.operationId }
|
|
5619
6051
|
};
|
|
5620
6052
|
} catch (error) {
|
|
5621
|
-
const policy =
|
|
6053
|
+
const policy = qmindOrganizationPolicyError(error);
|
|
5622
6054
|
if (policy) return {
|
|
5623
6055
|
ok: false,
|
|
5624
6056
|
error: policy,
|
|
@@ -5733,7 +6165,7 @@ async function runScheduledTask(client, tool, rawInput, invocation) {
|
|
|
5733
6165
|
meta: { operationId: invocation.operationId }
|
|
5734
6166
|
};
|
|
5735
6167
|
} catch (error) {
|
|
5736
|
-
const policy =
|
|
6168
|
+
const policy = qmindOrganizationPolicyError(error);
|
|
5737
6169
|
if (policy) return {
|
|
5738
6170
|
ok: false,
|
|
5739
6171
|
error: policy,
|
|
@@ -5757,7 +6189,7 @@ function reject(code, message) {
|
|
|
5757
6189
|
throw new SourceOrganizationPreflightError(code, message);
|
|
5758
6190
|
}
|
|
5759
6191
|
function segment(value) {
|
|
5760
|
-
if (
|
|
6192
|
+
if (value.trim() !== value || !qmindSourceTitlePolicy(value).allowed) reject("invalid_input", "The source path must contain valid single path segments.");
|
|
5761
6193
|
return value;
|
|
5762
6194
|
}
|
|
5763
6195
|
function directoryPath(source) {
|
|
@@ -5834,9 +6266,14 @@ const cursorSchema = z.object({
|
|
|
5834
6266
|
relationship: z.enum([
|
|
5835
6267
|
"owned",
|
|
5836
6268
|
"shared",
|
|
6269
|
+
"featured",
|
|
5837
6270
|
"all"
|
|
5838
6271
|
]),
|
|
5839
|
-
collection: z.enum([
|
|
6272
|
+
collection: z.enum([
|
|
6273
|
+
"owned",
|
|
6274
|
+
"shared",
|
|
6275
|
+
"featured"
|
|
6276
|
+
]),
|
|
5840
6277
|
page: z.number().int().min(1).max(1e6),
|
|
5841
6278
|
offset: z.number().int().min(0).max(49)
|
|
5842
6279
|
}).strict();
|
|
@@ -5848,7 +6285,7 @@ function initialPosition(input) {
|
|
|
5848
6285
|
if (input.cursor === void 0) return {
|
|
5849
6286
|
version: 1,
|
|
5850
6287
|
relationship,
|
|
5851
|
-
collection: relationship === "
|
|
6288
|
+
collection: relationship === "all" ? "owned" : relationship,
|
|
5852
6289
|
page: 1,
|
|
5853
6290
|
offset: 0
|
|
5854
6291
|
};
|
|
@@ -5861,10 +6298,11 @@ function initialPosition(input) {
|
|
|
5861
6298
|
return invalidCursor();
|
|
5862
6299
|
}
|
|
5863
6300
|
}
|
|
5864
|
-
/**
|
|
6301
|
+
/** 动态目录依次遍历 owned、shared、featured;游标只记录位置,每页重新经过 ACL 与宿主过滤。 */
|
|
5865
6302
|
async function discoverNotebooks(client, input, options, includes) {
|
|
5866
6303
|
let position = initialPosition(input);
|
|
5867
|
-
if (position.relationship
|
|
6304
|
+
if (["shared", "all"].includes(position.relationship) && client.listSharedNotebooks === void 0) throw Object.assign(/* @__PURE__ */ new Error("shared notebook listing is unavailable"), { code: "UNSUPPORTED_OPERATION" });
|
|
6305
|
+
if (["featured", "all"].includes(position.relationship) && client.listFeaturedNotebooks === void 0) throw Object.assign(/* @__PURE__ */ new Error("featured notebook listing is unavailable"), { code: "UNSUPPORTED_OPERATION" });
|
|
5868
6306
|
const notebooks = [];
|
|
5869
6307
|
const seen = /* @__PURE__ */ new Set();
|
|
5870
6308
|
for (let requests = 0; position && requests < maxPagesPerCall && notebooks.length < input.pageSize; requests += 1) {
|
|
@@ -5878,7 +6316,7 @@ async function discoverNotebooks(client, input, options, includes) {
|
|
|
5878
6316
|
pageSize: upstreamPageSize,
|
|
5879
6317
|
retrievalEligibleOnly: true
|
|
5880
6318
|
};
|
|
5881
|
-
const response = collection === "owned" ? await client.listNotebooks(pagination, options) : await client.listSharedNotebooks(pagination, options);
|
|
6319
|
+
const response = collection === "owned" ? await client.listNotebooks(pagination, options) : collection === "shared" ? await client.listSharedNotebooks(pagination, options) : await client.listFeaturedNotebooks(pagination, options);
|
|
5882
6320
|
options.signal?.throwIfAborted();
|
|
5883
6321
|
const items = response.notebooks.map((item) => "notebook" in item ? item.notebook : item);
|
|
5884
6322
|
if (items.length > upstreamPageSize || items.length > 0 && (response.currentPage !== void 0 && response.currentPage !== page || response.pageSize !== void 0 && response.pageSize !== upstreamPageSize)) throw Object.assign(/* @__PURE__ */ new Error("invalid notebook page"), { code: "REMOTE_ERROR" });
|
|
@@ -5901,9 +6339,9 @@ async function discoverNotebooks(client, input, options, includes) {
|
|
|
5901
6339
|
page: page + 1,
|
|
5902
6340
|
offset: 0
|
|
5903
6341
|
};
|
|
5904
|
-
else position = collection
|
|
6342
|
+
else position = collection !== "featured" && relationship === "all" ? {
|
|
5905
6343
|
...position,
|
|
5906
|
-
collection: "shared",
|
|
6344
|
+
collection: collection === "owned" ? "shared" : "featured",
|
|
5907
6345
|
page: 1,
|
|
5908
6346
|
offset: 0
|
|
5909
6347
|
} : void 0;
|
|
@@ -6078,7 +6516,20 @@ function chunkData(chunk) {
|
|
|
6078
6516
|
score: chunk.score,
|
|
6079
6517
|
sourceId: chunk.sourceId,
|
|
6080
6518
|
sourceTitle: chunk.sourceTitle,
|
|
6081
|
-
tokenCount: chunk.tokenCount
|
|
6519
|
+
tokenCount: chunk.tokenCount,
|
|
6520
|
+
...chunkLineRange(chunk.metadata)
|
|
6521
|
+
};
|
|
6522
|
+
}
|
|
6523
|
+
function chunkLineRange(metadata) {
|
|
6524
|
+
const positive = (value) => {
|
|
6525
|
+
const number = typeof value === "string" && value.trim() ? Number(value) : value;
|
|
6526
|
+
return typeof number === "number" && Number.isSafeInteger(number) && number > 0 ? number : void 0;
|
|
6527
|
+
};
|
|
6528
|
+
const lineStart = positive(metadata?.lineStart ?? metadata?.line_start);
|
|
6529
|
+
const lineEnd = positive(metadata?.lineEnd ?? metadata?.line_end);
|
|
6530
|
+
return lineStart === void 0 ? {} : {
|
|
6531
|
+
lineStart,
|
|
6532
|
+
...lineEnd !== void 0 && lineEnd >= lineStart ? { lineEnd } : {}
|
|
6082
6533
|
};
|
|
6083
6534
|
}
|
|
6084
6535
|
function chunkPromptContext(chunks) {
|
|
@@ -6089,18 +6540,58 @@ async function retrieve(client, input, invocation) {
|
|
|
6089
6540
|
notebookId: input.notebookId,
|
|
6090
6541
|
...input.sourceIds === void 0 ? {} : { sourceIds: input.sourceIds }
|
|
6091
6542
|
}]);
|
|
6092
|
-
if (targets === void 0 || targets.length === 0 || targets.length > 5) return failure$1(invocation.operationId, "selection_required", "Use list_notebooks overview to select one to five relevant notebooks.");
|
|
6093
|
-
const
|
|
6094
|
-
|
|
6095
|
-
|
|
6096
|
-
|
|
6097
|
-
|
|
6098
|
-
|
|
6099
|
-
|
|
6100
|
-
|
|
6101
|
-
|
|
6102
|
-
|
|
6103
|
-
|
|
6543
|
+
if (targets === void 0 || targets.length === 0 || targets.length > 5 && invocation.defaultRetrievalMode !== "all") return failure$1(invocation.operationId, "selection_required", "Use list_notebooks overview to select one to five relevant notebooks.");
|
|
6544
|
+
const batches = [];
|
|
6545
|
+
const batchSize = invocation.defaultRetrievalMode === "all" ? 1 : 5;
|
|
6546
|
+
for (let offset = 0; offset < targets.length; offset += batchSize) batches.push(targets.slice(offset, offset + batchSize));
|
|
6547
|
+
const results = [];
|
|
6548
|
+
const warnings = [];
|
|
6549
|
+
const request = async (batch) => {
|
|
6550
|
+
return client.retrieveNotebooks({
|
|
6551
|
+
targets: batch.map(({ notebookId, sourceIds }) => ({
|
|
6552
|
+
notebookId,
|
|
6553
|
+
...sourceIds === void 0 ? {} : { sourceIds },
|
|
6554
|
+
...notebookSelection(invocation, notebookId) === "preferred" ? { preference: "preferred" } : {}
|
|
6555
|
+
})),
|
|
6556
|
+
maxResults: input.maxResults,
|
|
6557
|
+
query: input.query,
|
|
6558
|
+
...input.scoreThreshold === void 0 ? {} : { scoreThreshold: input.scoreThreshold },
|
|
6559
|
+
...input.topK === void 0 ? {} : { topK: input.topK }
|
|
6560
|
+
}, callOptions(invocation));
|
|
6561
|
+
};
|
|
6562
|
+
for (let offset = 0; offset < batches.length; offset += 3) {
|
|
6563
|
+
invocation.signal?.throwIfAborted();
|
|
6564
|
+
const current = batches.slice(offset, offset + 3);
|
|
6565
|
+
const settled = await Promise.allSettled(current.map(request));
|
|
6566
|
+
invocation.signal?.throwIfAborted();
|
|
6567
|
+
invocation.assertCurrent?.();
|
|
6568
|
+
settled.forEach((entry, index) => {
|
|
6569
|
+
if (entry.status === "fulfilled") results.push(entry.value);
|
|
6570
|
+
else {
|
|
6571
|
+
if (batches.length === 1 || qmindOrganizationPolicyError(entry.reason) || ["unauthenticated", "permission_denied"].includes(errorCode(entry.reason))) throw entry.reason;
|
|
6572
|
+
for (const target of current[index]) warnings.push({
|
|
6573
|
+
code: "notebook_search_failed",
|
|
6574
|
+
notebookId: target.notebookId
|
|
6575
|
+
});
|
|
6576
|
+
}
|
|
6577
|
+
});
|
|
6578
|
+
}
|
|
6579
|
+
if (results.length === 0) return failure$1(invocation.operationId, "upstream_unavailable", "Notebook retrieval failed.");
|
|
6580
|
+
const result = {
|
|
6581
|
+
chunks: results.flatMap((item) => item.chunks.map((chunk) => {
|
|
6582
|
+
if (chunkLineRange(chunk.metadata).lineStart !== void 0) return chunk;
|
|
6583
|
+
const citation = item.citations.find((entry) => entry.notebookId === chunk.notebookId && entry.sourceId === chunk.sourceId && (entry.chunkId === chunk.chunkId || entry.chunkIndex === chunk.chunkIndex) && chunkLineRange(entry.metadata).lineStart !== void 0);
|
|
6584
|
+
return citation ? {
|
|
6585
|
+
...chunk,
|
|
6586
|
+
metadata: {
|
|
6587
|
+
...chunk.metadata,
|
|
6588
|
+
...chunkLineRange(citation.metadata)
|
|
6589
|
+
}
|
|
6590
|
+
} : chunk;
|
|
6591
|
+
})).sort((a, b) => b.score - a.score),
|
|
6592
|
+
total: results.reduce((sum, item) => sum + item.total, 0),
|
|
6593
|
+
reranked: results.length === 1 && results[0].reranked
|
|
6594
|
+
};
|
|
6104
6595
|
const bounded = result.chunks.slice(0, input.maxResults);
|
|
6105
6596
|
return {
|
|
6106
6597
|
data: {
|
|
@@ -6109,7 +6600,13 @@ async function retrieve(client, input, invocation) {
|
|
|
6109
6600
|
reranked: result.reranked,
|
|
6110
6601
|
total: result.total
|
|
6111
6602
|
},
|
|
6112
|
-
meta: {
|
|
6603
|
+
meta: {
|
|
6604
|
+
operationId: invocation.operationId,
|
|
6605
|
+
...warnings.length ? {
|
|
6606
|
+
partial: true,
|
|
6607
|
+
warnings
|
|
6608
|
+
} : {}
|
|
6609
|
+
},
|
|
6113
6610
|
ok: true
|
|
6114
6611
|
};
|
|
6115
6612
|
}
|
|
@@ -6121,6 +6618,7 @@ async function listNotebooks(client, input, invocation, allowedNotebookIds) {
|
|
|
6121
6618
|
if (![
|
|
6122
6619
|
"owned",
|
|
6123
6620
|
"shared",
|
|
6621
|
+
"featured",
|
|
6124
6622
|
"all"
|
|
6125
6623
|
].includes(relationship)) throw new TypeError("Invalid default Notebook relationship.");
|
|
6126
6624
|
const result = allowed?.size === 0 ? {
|
|
@@ -6212,7 +6710,8 @@ async function getSource(client, input, invocation) {
|
|
|
6212
6710
|
};
|
|
6213
6711
|
}
|
|
6214
6712
|
async function readSource(client, input, invocation) {
|
|
6215
|
-
const
|
|
6713
|
+
const source = await client.getSource(input.notebookId, input.sourceId, callOptions(invocation));
|
|
6714
|
+
const status = normalizedSourceStatus(source.status);
|
|
6216
6715
|
if (status === "pending" || status === "processing") return failure$1(invocation.operationId, "source_not_ready", "The source is not ready to read.");
|
|
6217
6716
|
if (status === "unsupported") return failure$1(invocation.operationId, "source_unsupported", "The source format is unsupported.");
|
|
6218
6717
|
if (status === "failed") return failure$1(invocation.operationId, "source_failed", "The source failed during ingestion.");
|
|
@@ -6226,6 +6725,8 @@ async function readSource(client, input, invocation) {
|
|
|
6226
6725
|
contentType: result.contentType,
|
|
6227
6726
|
...truncated ? { nextOffset } : {},
|
|
6228
6727
|
sourceId: input.sourceId,
|
|
6728
|
+
notebookId: input.notebookId,
|
|
6729
|
+
updatedAt: source.updatedAt,
|
|
6229
6730
|
truncated
|
|
6230
6731
|
},
|
|
6231
6732
|
meta: { operationId: invocation.operationId },
|
|
@@ -6237,7 +6738,11 @@ async function addSource(client, input, invocation) {
|
|
|
6237
6738
|
if (cancelledBeforeMutation !== void 0) return failure$1(invocation.operationId, cancelledBeforeMutation, cancelledBeforeMutation === "timeout" ? "The source mutation timed out before it was sent." : "The source mutation was cancelled before it was sent.");
|
|
6238
6739
|
try {
|
|
6239
6740
|
if (input.source.kind === "url") {
|
|
6240
|
-
const imported = (await client.importWebLinks(input.notebookId, {
|
|
6741
|
+
const imported = (await client.importWebLinks(input.notebookId, {
|
|
6742
|
+
urls: [input.source.url],
|
|
6743
|
+
...input.parentSourceId === void 0 ? {} : { parentSourceId: input.parentSourceId },
|
|
6744
|
+
...input.path === void 0 ? {} : { path: input.path }
|
|
6745
|
+
}, callOptions(invocation)))[0];
|
|
6241
6746
|
if (imported?.source === void 0) {
|
|
6242
6747
|
if (imported?.error !== void 0) return failure$1(invocation.operationId, "upstream_unavailable", "QMind could not import the URL source.");
|
|
6243
6748
|
return failure$1(invocation.operationId, "outcome_unknown", "The URL source result could not be confirmed.");
|
|
@@ -6248,22 +6753,36 @@ async function addSource(client, input, invocation) {
|
|
|
6248
6753
|
ok: true
|
|
6249
6754
|
};
|
|
6250
6755
|
}
|
|
6251
|
-
const
|
|
6756
|
+
const textBytes = input.source.kind === "text" ? new TextEncoder().encode(input.source.content) : void 0;
|
|
6757
|
+
const binary = textBytes === void 0 ? invocation.fileSource : {
|
|
6758
|
+
filename: "content.md",
|
|
6759
|
+
mimeType: "text/markdown",
|
|
6760
|
+
size: textBytes.byteLength,
|
|
6761
|
+
async *open() {
|
|
6762
|
+
yield textBytes;
|
|
6763
|
+
}
|
|
6764
|
+
};
|
|
6252
6765
|
if (binary === void 0) return failure$1(invocation.operationId, "internal", "The local file source was not prepared.");
|
|
6253
6766
|
const title = input.source.title;
|
|
6254
6767
|
return {
|
|
6255
|
-
data: sourceData(await client.uploadSource(input.notebookId, binary,
|
|
6768
|
+
data: sourceData(await client.uploadSource(input.notebookId, binary, {
|
|
6769
|
+
...title === void 0 ? {} : { title },
|
|
6770
|
+
sourceType: input.source.kind === "text" ? "markdown" : "file",
|
|
6771
|
+
...input.parentSourceId === void 0 ? {} : { parentSourceId: input.parentSourceId },
|
|
6772
|
+
...input.path === void 0 ? {} : { path: input.path }
|
|
6773
|
+
}, callOptions(invocation))),
|
|
6256
6774
|
meta: { operationId: invocation.operationId },
|
|
6257
6775
|
ok: true
|
|
6258
6776
|
};
|
|
6259
6777
|
} catch (error) {
|
|
6260
|
-
const policyError =
|
|
6778
|
+
const policyError = qmindOrganizationPolicyError(error);
|
|
6261
6779
|
if (policyError) return {
|
|
6262
6780
|
ok: false,
|
|
6263
6781
|
error: policyError,
|
|
6264
6782
|
meta: { operationId: invocation.operationId }
|
|
6265
6783
|
};
|
|
6266
|
-
const
|
|
6784
|
+
const mapped = errorCode(error, true);
|
|
6785
|
+
const code = mapped === "internal" ? "outcome_unknown" : mapped;
|
|
6267
6786
|
return failure$1(invocation.operationId, code, code === "outcome_unknown" ? "The source mutation outcome is unknown." : "QMind could not add the source.");
|
|
6268
6787
|
}
|
|
6269
6788
|
}
|
|
@@ -6311,7 +6830,7 @@ async function deleteSource(client, input, invocation) {
|
|
|
6311
6830
|
}
|
|
6312
6831
|
};
|
|
6313
6832
|
} catch (error) {
|
|
6314
|
-
const policyError =
|
|
6833
|
+
const policyError = qmindOrganizationPolicyError(error);
|
|
6315
6834
|
if (policyError) return {
|
|
6316
6835
|
ok: false,
|
|
6317
6836
|
error: policyError,
|
|
@@ -6361,7 +6880,7 @@ async function organizeSource(client, tool, input, invocation) {
|
|
|
6361
6880
|
meta: { operationId: invocation.operationId }
|
|
6362
6881
|
};
|
|
6363
6882
|
} catch (error) {
|
|
6364
|
-
const policyError =
|
|
6883
|
+
const policyError = qmindOrganizationPolicyError(error);
|
|
6365
6884
|
if (policyError) return {
|
|
6366
6885
|
ok: false,
|
|
6367
6886
|
error: policyError,
|
|
@@ -6371,6 +6890,16 @@ async function organizeSource(client, tool, input, invocation) {
|
|
|
6371
6890
|
return failure$1(invocation.operationId, mutationStarted && code === "internal" ? "outcome_unknown" : code, mutationStarted ? "The source operation did not return a confirmed result. Inspect the current source location before another call; do not retry automatically." : error instanceof SourceOrganizationPreflightError ? error.message : "The source operation failed before submission.");
|
|
6372
6891
|
}
|
|
6373
6892
|
}
|
|
6893
|
+
function validateQMindAgentInvocation(toolId, input, invocation) {
|
|
6894
|
+
const parsed = agentToolInputSchemas[toolId].safeParse(input);
|
|
6895
|
+
if (!parsed.success) return failure$1(invocation.operationId, "invalid_input", "The tool input is invalid.");
|
|
6896
|
+
try {
|
|
6897
|
+
invocation.assertCurrent?.();
|
|
6898
|
+
return enforceNotebookScope(toolId, parsed.data, invocation, scopedNotebookIds(invocation));
|
|
6899
|
+
} catch {
|
|
6900
|
+
return failure$1(invocation.operationId, "permission_denied", "The host context is no longer valid.");
|
|
6901
|
+
}
|
|
6902
|
+
}
|
|
6374
6903
|
function createQMindAgentCore(client) {
|
|
6375
6904
|
if (typeof client !== "object" || client === null) throw new TypeError("client is required");
|
|
6376
6905
|
return Object.freeze({ async invoke(toolId, rawInput, invocation) {
|
|
@@ -6381,7 +6910,11 @@ function createQMindAgentCore(client) {
|
|
|
6381
6910
|
const allowedNotebookIds = scopedNotebookIds(invocation);
|
|
6382
6911
|
const denied = enforceNotebookScope(toolId, parsed.data, invocation, allowedNotebookIds);
|
|
6383
6912
|
if (denied !== void 0) return denied;
|
|
6913
|
+
invocation.assertCurrent?.();
|
|
6384
6914
|
switch (toolId) {
|
|
6915
|
+
case "qmind.update_source_content": return await updateSourceContent(client, parsed.data, invocation);
|
|
6916
|
+
case "qmind.refresh_source": return await refreshSource(client, parsed.data, invocation);
|
|
6917
|
+
case "qmind.list_task_runs": return await listTaskRuns(client, parsed.data, invocation);
|
|
6385
6918
|
case "qmind.create_notebook":
|
|
6386
6919
|
case "qmind.delete_notebook": return await runNotebookMutation(client, toolId, parsed.data, invocation);
|
|
6387
6920
|
case "qmind.create_scheduled_task":
|
|
@@ -6399,7 +6932,7 @@ function createQMindAgentCore(client) {
|
|
|
6399
6932
|
case "qmind.add_source": return await addSource(client, parsed.data, invocation);
|
|
6400
6933
|
}
|
|
6401
6934
|
} catch (error) {
|
|
6402
|
-
const policyError =
|
|
6935
|
+
const policyError = qmindOrganizationPolicyError(error);
|
|
6403
6936
|
if (policyError) return {
|
|
6404
6937
|
ok: false,
|
|
6405
6938
|
error: policyError,
|
|
@@ -6485,31 +7018,42 @@ async function runQMindAgentOperation(dependencies, toolId, input, runtimeSignal
|
|
|
6485
7018
|
const code = abortCode(signal, timedOut);
|
|
6486
7019
|
result = failure(operationId, code, code === "timeout" ? "The QMind operation timed out." : "The QMind operation was cancelled.");
|
|
6487
7020
|
} else {
|
|
6488
|
-
|
|
6489
|
-
|
|
6490
|
-
|
|
6491
|
-
|
|
6492
|
-
|
|
6493
|
-
|
|
6494
|
-
|
|
6495
|
-
|
|
6496
|
-
|
|
6497
|
-
|
|
6498
|
-
|
|
6499
|
-
|
|
6500
|
-
|
|
6501
|
-
|
|
6502
|
-
|
|
6503
|
-
|
|
6504
|
-
|
|
6505
|
-
|
|
6506
|
-
|
|
6507
|
-
|
|
6508
|
-
|
|
6509
|
-
|
|
6510
|
-
|
|
6511
|
-
|
|
6512
|
-
|
|
7021
|
+
const denied = validateQMindAgentInvocation(toolId, input, {
|
|
7022
|
+
...operationContext,
|
|
7023
|
+
operationId,
|
|
7024
|
+
...dependencies.defaultNotebookIds === void 0 ? {} : { defaultNotebookIds: dependencies.defaultNotebookIds }
|
|
7025
|
+
});
|
|
7026
|
+
if (denied) result = denied;
|
|
7027
|
+
else {
|
|
7028
|
+
let fileSource;
|
|
7029
|
+
const requestedPath = filePath(toolId, input);
|
|
7030
|
+
if (requestedPath !== void 0) try {
|
|
7031
|
+
signal.throwIfAborted();
|
|
7032
|
+
fileSource = await dependencies.createFileSource(requestedPath);
|
|
7033
|
+
signal.throwIfAborted();
|
|
7034
|
+
} catch (error) {
|
|
7035
|
+
const code = signal.aborted ? abortCode(signal, timedOut) : fileSourceErrorCode(error);
|
|
7036
|
+
result = failure(operationId, code, code === "invalid_input" ? "The local file could not be read." : code === "timeout" ? "The QMind operation timed out." : code === "cancelled" ? "The QMind operation was cancelled." : "The local file source failed.");
|
|
7037
|
+
}
|
|
7038
|
+
if (requestedPath === void 0 || fileSource !== void 0) try {
|
|
7039
|
+
operationContext.assertCurrent?.();
|
|
7040
|
+
result = await dependencies.core.invoke(toolId, input, {
|
|
7041
|
+
assertCurrent: operationContext.assertCurrent,
|
|
7042
|
+
defaultRetrievalMode: dependencies.defaultRetrievalMode,
|
|
7043
|
+
...operationContext.notebookPolicy === void 0 ? {} : { notebookPolicy: operationContext.notebookPolicy },
|
|
7044
|
+
...operationContext.defaultNotebookRelationship === void 0 ? {} : { defaultNotebookRelationship: operationContext.defaultNotebookRelationship },
|
|
7045
|
+
...fileSource === void 0 ? {} : { fileSource },
|
|
7046
|
+
...dependencies.defaultNotebookIds === void 0 ? {} : { defaultNotebookIds: dependencies.defaultNotebookIds },
|
|
7047
|
+
...operationContext.approvedDeletionTargets === void 0 ? {} : { approvedDeletionTargets: operationContext.approvedDeletionTargets },
|
|
7048
|
+
...operationContext.notebookScope === void 0 ? {} : { notebookScope: operationContext.notebookScope },
|
|
7049
|
+
operationId,
|
|
7050
|
+
signal,
|
|
7051
|
+
...perRequestToken === void 0 ? {} : { token: perRequestToken }
|
|
7052
|
+
});
|
|
7053
|
+
if (timedOut && definition.intent === "read" && result.ok === false && result.error.code === "cancelled") result = failure(operationId, "timeout", "The QMind operation timed out.");
|
|
7054
|
+
} catch {
|
|
7055
|
+
result = failure(operationId, "internal", "The QMind operation failed.");
|
|
7056
|
+
}
|
|
6513
7057
|
}
|
|
6514
7058
|
}
|
|
6515
7059
|
} finally {
|
|
@@ -6662,11 +7206,32 @@ function registerCanonicalCommands(context, program) {
|
|
|
6662
7206
|
//#region src/application.ts
|
|
6663
7207
|
const distribution = CLI_DISTRIBUTIONS[QMIND_CLI_DISTRIBUTION];
|
|
6664
7208
|
function registerCoreCommands(context, program) {
|
|
6665
|
-
withCommon(program.command("login").description("
|
|
6666
|
-
const host = await hostFor(context, raw);
|
|
7209
|
+
withCommon(program.command("login").description("Sign in with a Qoder PAT or browser and save credentials"), false).option("--client-id <id>", "OAuth App client_id").option("--pat <token>", "Qoder personal access token (prefer --pat-stdin or environment variables)").option("--pat-stdin", "read a Qoder personal access token from standard input").option("--no-browser", "print the browser authorization URL without opening it").action(async (raw) => {
|
|
6667
7210
|
const clientId = optionalString(raw, "clientId");
|
|
6668
|
-
|
|
6669
|
-
|
|
7211
|
+
if (raw.patStdin === true && (raw.pat !== void 0 || raw.token !== void 0)) throw new QMindError("INVALID_ARGUMENT", "--pat-stdin cannot be combined with --pat or --token");
|
|
7212
|
+
if (raw.pat !== void 0 && raw.token !== void 0) throw new QMindError("INVALID_ARGUMENT", "--pat cannot be combined with --token");
|
|
7213
|
+
let personalToken = stringOption(raw, "pat");
|
|
7214
|
+
if (raw.patStdin === true) {
|
|
7215
|
+
if (context.io.stdinIsTTY) throw new QMindError("INVALID_ARGUMENT", "--pat-stdin requires piped or redirected input");
|
|
7216
|
+
const readStdin = context.io.readStdin ?? processIO().readStdin;
|
|
7217
|
+
if (readStdin === void 0) throw new QMindError("INVALID_ARGUMENT", "stdin reader is unavailable");
|
|
7218
|
+
personalToken = await readStdin();
|
|
7219
|
+
}
|
|
7220
|
+
if (personalToken !== void 0) {
|
|
7221
|
+
personalToken = personalToken.trim();
|
|
7222
|
+
if (personalToken.length > 16384 || !personalToken.startsWith("pt-") || personalToken.length <= 3 || /\s/.test(personalToken)) throw new QMindError("INVALID_ARGUMENT", "Qoder PAT must be a single non-empty pt- token (maximum 16 KiB)");
|
|
7223
|
+
if (clientId !== void 0) throw new QMindError("INVALID_ARGUMENT", "--client-id cannot be combined with PAT login");
|
|
7224
|
+
}
|
|
7225
|
+
const host = await hostFor(context, personalToken === void 0 ? raw : {
|
|
7226
|
+
...raw,
|
|
7227
|
+
token: personalToken
|
|
7228
|
+
});
|
|
7229
|
+
const result = await host.login({
|
|
7230
|
+
...clientId === void 0 ? {} : { clientId },
|
|
7231
|
+
...personalToken === void 0 ? {} : { personalToken },
|
|
7232
|
+
...raw.browser === false ? { openBrowser: false } : {}
|
|
7233
|
+
});
|
|
7234
|
+
context.io.stdout(`Logged in successfully [${result.sashOrigin} -> ${host.config.dashboardOrigin}]\nToken saved to ${host.config.homeDirectory}/credentials.json\n` + (result.expiresAt.length === 0 ? "" : `Access token expires at: ${result.expiresAt}\n`));
|
|
6670
7235
|
});
|
|
6671
7236
|
withCommon(program.command("logout").description("Remove saved credentials"), false).action(async (raw) => {
|
|
6672
7237
|
await (await hostFor(context, raw)).logout();
|
|
@@ -7116,7 +7681,7 @@ async function runQMindCli(argv, options = {}) {
|
|
|
7116
7681
|
updater: options.updater ?? createQMindCliUpdater({ identity: {
|
|
7117
7682
|
artifactKind: "npm",
|
|
7118
7683
|
command: distribution.command,
|
|
7119
|
-
currentVersion: "4.
|
|
7684
|
+
currentVersion: "4.2.0",
|
|
7120
7685
|
distribution: distribution.id,
|
|
7121
7686
|
executablePath: process.execPath,
|
|
7122
7687
|
package: distribution.package,
|