@capybara-hq/kernel 0.1.0-rc.103 → 0.1.0-rc.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/conversations/conversations.service.d.ts +2 -0
- package/dist/conversations/conversations.service.js +6 -1
- package/dist/conversations/events.d.ts +12 -0
- package/dist/conversations/events.js +23 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +13 -0
- package/dist/evals/reconstruct.d.ts +9 -1
- package/dist/evals/reconstruct.js +7 -0
- package/dist/handoff/app-schemes.d.ts +28 -0
- package/dist/handoff/app-schemes.js +41 -0
- package/dist/handoff/handoff-paths.d.ts +26 -0
- package/dist/handoff/handoff-paths.js +25 -0
- package/dist/handoff/handoff-tickets.d.ts +28 -0
- package/dist/handoff/handoff-tickets.js +111 -0
- package/dist/handoff/handoff.controller.d.ts +24 -0
- package/dist/handoff/handoff.controller.js +74 -0
- package/dist/handoff/handoff.module.d.ts +7 -0
- package/dist/handoff/handoff.module.js +26 -0
- package/dist/handoff/handoff.schemas.d.ts +13 -0
- package/dist/handoff/handoff.schemas.js +3 -0
- package/dist/handoff/handoff.service.d.ts +49 -0
- package/dist/handoff/handoff.service.js +96 -0
- package/dist/http/api.module.js +8 -0
- package/dist/http/meta.controller.d.ts +1 -0
- package/dist/http/meta.controller.js +7 -0
- package/dist/identity/sso.controller.d.ts +30 -6
- package/dist/identity/sso.controller.js +51 -17
- package/dist/identity/sso.d.ts +10 -2
- package/dist/identity/sso.js +15 -3
- package/dist/overrides.d.ts +4 -4
- package/dist/overrides.js +1 -1
- package/dist/platform/exclusions.js +5 -0
- package/dist/platform/ops/connectors.ops.js +4 -2
- package/dist/runtime/compaction.service.d.ts +6 -3
- package/dist/runtime/compaction.service.js +7 -6
- package/dist/runtime/context/build.d.ts +13 -3
- package/dist/runtime/context/build.js +26 -6
- package/dist/runtime/context/compaction.d.ts +29 -1
- package/dist/runtime/context/compaction.js +108 -39
- package/dist/runtime/context/run-outcome.d.ts +10 -0
- package/dist/runtime/context/run-outcome.js +92 -0
- package/dist/runtime/model-call.d.ts +9 -0
- package/dist/runtime/model-call.js +63 -7
- package/dist/runtime/project/knowledge.service.d.ts +16 -7
- package/dist/runtime/project/knowledge.service.js +31 -13
- package/dist/tools/connectors.service.d.ts +4 -13
- package/dist/tools/connectors.service.js +12 -21
- package/dist/tools/oauth/flow.d.ts +16 -2
- package/dist/tools/oauth/flow.js +23 -4
- package/dist/tools/oauth/oauth.controller.d.ts +19 -7
- package/dist/tools/oauth/oauth.controller.js +42 -12
- package/dist/tools/oauth/oauth.schemas.d.ts +14 -1
- package/dist/tools/oauth/oauth.schemas.js +4 -1
- package/package.json +3 -3
|
@@ -6,13 +6,22 @@ import { TransactionHost } from "../../infra/database/transaction-host.js";
|
|
|
6
6
|
import type { KernelLogger } from "../../log.js";
|
|
7
7
|
import { SandboxService } from "../../sandbox/sandbox.service.js";
|
|
8
8
|
/**
|
|
9
|
-
* The
|
|
10
|
-
*
|
|
9
|
+
* The time limits of a run start's project read. The device's start (a resume, or a recreate when
|
|
10
|
+
* its instance is gone) and the read itself are bounded apart, so a slow start never eats the
|
|
11
|
+
* read's time: past either the run goes on without a new snapshot, as when the device is
|
|
12
|
+
* unreachable.
|
|
11
13
|
*/
|
|
12
|
-
export
|
|
14
|
+
export interface ProjectKnowledgeTimeouts {
|
|
15
|
+
/** Entering the device: from the read's start until the device runs. */
|
|
16
|
+
startMs: number;
|
|
17
|
+
/** Every device call of the read once the device runs, under one bound. */
|
|
18
|
+
readMs: number;
|
|
19
|
+
}
|
|
20
|
+
/** The default {@link ProjectKnowledgeTimeouts}. */
|
|
21
|
+
export declare const PROJECT_KNOWLEDGE_TIMEOUTS: ProjectKnowledgeTimeouts;
|
|
13
22
|
/**
|
|
14
|
-
* Test seam (`KernelSeams`): the read's time
|
|
15
|
-
* {@link
|
|
23
|
+
* Test seam (`KernelSeams`): the read's time limits, asked at every read; default
|
|
24
|
+
* {@link PROJECT_KNOWLEDGE_TIMEOUTS}.
|
|
16
25
|
*/
|
|
17
26
|
export declare const PROJECT_KNOWLEDGE_TIMEOUT: unique symbol;
|
|
18
27
|
/** What a run start tells {@link ProjectKnowledgeService.record}. */
|
|
@@ -62,8 +71,8 @@ export declare class ProjectKnowledgeService {
|
|
|
62
71
|
private readonly projects;
|
|
63
72
|
private readonly sandbox;
|
|
64
73
|
private readonly log;
|
|
65
|
-
private readonly
|
|
66
|
-
constructor(db: KernelDb, eventLog: Pick<EventLog, "appendIn" | "notify">, txHost: TransactionHost, devices: Pick<ConversationDevices, "forTool">, projects: Pick<DeviceProjectsService, "scopeFor" | "keeps">, sandbox: Pick<SandboxService, "withKernelRead">, log: KernelLogger,
|
|
74
|
+
private readonly timeouts;
|
|
75
|
+
constructor(db: KernelDb, eventLog: Pick<EventLog, "appendIn" | "notify">, txHost: TransactionHost, devices: Pick<ConversationDevices, "forTool">, projects: Pick<DeviceProjectsService, "scopeFor" | "keeps">, sandbox: Pick<SandboxService, "withKernelRead">, log: KernelLogger, timeouts?: () => ProjectKnowledgeTimeouts);
|
|
67
76
|
/**
|
|
68
77
|
* Reads and records (see the class); notifies after the commit. False: the run had ended under
|
|
69
78
|
* the locks (a stop landed while the device was read), so nothing was appended and the run must
|
|
@@ -27,16 +27,18 @@ import { providerConnectionFailure } from "../../sandbox/unreachable.js";
|
|
|
27
27
|
import { currentExecutionClaim } from "../run-execution.js";
|
|
28
28
|
import { runEndedIn } from "../run-status.js";
|
|
29
29
|
import { projectRootIdemKey, projectUnavailable, readProjectKnowledge, sameProjectRoot, unavailableProject, } from "./knowledge.js";
|
|
30
|
+
/** The default {@link ProjectKnowledgeTimeouts}. */
|
|
31
|
+
export const PROJECT_KNOWLEDGE_TIMEOUTS = {
|
|
32
|
+
startMs: 60_000,
|
|
33
|
+
readMs: 30_000,
|
|
34
|
+
};
|
|
30
35
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*/
|
|
34
|
-
export const PROJECT_KNOWLEDGE_TIMEOUT_MS = 30_000;
|
|
35
|
-
/**
|
|
36
|
-
* Test seam (`KernelSeams`): the read's time limit in ms, asked at every read; default
|
|
37
|
-
* {@link PROJECT_KNOWLEDGE_TIMEOUT_MS}.
|
|
36
|
+
* Test seam (`KernelSeams`): the read's time limits, asked at every read; default
|
|
37
|
+
* {@link PROJECT_KNOWLEDGE_TIMEOUTS}.
|
|
38
38
|
*/
|
|
39
39
|
export const PROJECT_KNOWLEDGE_TIMEOUT = Symbol("PROJECT_KNOWLEDGE_TIMEOUT");
|
|
40
|
+
/** The abort reason of a passed time limit (as `AbortSignal.timeout` gives). */
|
|
41
|
+
const timeoutReason = (what) => new DOMException(`The project read's ${what} time limit passed`, "TimeoutError");
|
|
40
42
|
/** The run start has no current execution claim of its own run: nothing is admitted. */
|
|
41
43
|
class KnowledgeClaimMissing extends Error {
|
|
42
44
|
constructor() {
|
|
@@ -91,8 +93,8 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
91
93
|
projects;
|
|
92
94
|
sandbox;
|
|
93
95
|
log;
|
|
94
|
-
|
|
95
|
-
constructor(db, eventLog, txHost, devices, projects, sandbox, log,
|
|
96
|
+
timeouts;
|
|
97
|
+
constructor(db, eventLog, txHost, devices, projects, sandbox, log, timeouts = () => PROJECT_KNOWLEDGE_TIMEOUTS) {
|
|
96
98
|
this.db = db;
|
|
97
99
|
this.eventLog = eventLog;
|
|
98
100
|
this.txHost = txHost;
|
|
@@ -100,7 +102,7 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
100
102
|
this.projects = projects;
|
|
101
103
|
this.sandbox = sandbox;
|
|
102
104
|
this.log = log;
|
|
103
|
-
this.
|
|
105
|
+
this.timeouts = timeouts;
|
|
104
106
|
}
|
|
105
107
|
/**
|
|
106
108
|
* Reads and records (see the class); notifies after the commit. False: the run had ended under
|
|
@@ -169,7 +171,12 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
169
171
|
runId: input.runId,
|
|
170
172
|
project: bound.project,
|
|
171
173
|
};
|
|
172
|
-
const
|
|
174
|
+
const limits = this.timeouts();
|
|
175
|
+
// the start's limit runs until the device runs; the read then has its own
|
|
176
|
+
const start = new AbortController();
|
|
177
|
+
const startTimer = setTimeout(() => start.abort(timeoutReason("start")), limits.startMs);
|
|
178
|
+
const signal = AbortSignal.any([input.signal, start.signal]);
|
|
179
|
+
let readSignal;
|
|
173
180
|
try {
|
|
174
181
|
// the device a tool call of this conversation would enter, checked for its owner now
|
|
175
182
|
const deviceId = await this.devices.forTool(input.orgId, input.conversationId);
|
|
@@ -202,7 +209,10 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
202
209
|
conversationId: input.conversationId,
|
|
203
210
|
runId: input.runId,
|
|
204
211
|
},
|
|
205
|
-
}, async (h, p, info,
|
|
212
|
+
}, async (h, p, info, held) => {
|
|
213
|
+
clearTimeout(startTimer);
|
|
214
|
+
const sig = AbortSignal.any([held, AbortSignal.timeout(limits.readMs)]);
|
|
215
|
+
readSignal = sig;
|
|
206
216
|
const target = { project: bound.project, root: bound.root, mode: scope.mode };
|
|
207
217
|
// the kernel's own check first: a root above the owner's home is refused as the working
|
|
208
218
|
// root's rule refuses it (`workingRootOf`, the same home: the reported one, else the
|
|
@@ -253,13 +263,21 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
253
263
|
}, signal);
|
|
254
264
|
}
|
|
255
265
|
catch (err) {
|
|
266
|
+
const stopped = input.signal.aborted;
|
|
267
|
+
const readTimedOut = !stopped && readSignal?.aborted === true;
|
|
268
|
+
const startTimedOut = !stopped && start.signal.aborted;
|
|
256
269
|
this.log.warn("project knowledge not read: the device could not be asked; the run goes on", {
|
|
257
270
|
...where,
|
|
258
|
-
timedOut:
|
|
271
|
+
timedOut: readTimedOut || startTimedOut,
|
|
272
|
+
...(startTimedOut && { timedOutIn: "start" }),
|
|
273
|
+
...(readTimedOut && { timedOutIn: "read" }),
|
|
259
274
|
err,
|
|
260
275
|
});
|
|
261
276
|
return null;
|
|
262
277
|
}
|
|
278
|
+
finally {
|
|
279
|
+
clearTimeout(startTimer);
|
|
280
|
+
}
|
|
263
281
|
}
|
|
264
282
|
/**
|
|
265
283
|
* How the read enters the device: `withKernelRead` under the run's current execution claim (the
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type AuthHint, type ConnectorOperation, type GitSource, type ToolAnnotations } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import { type ConnectionGuideState, type GuideAck, PluginGuides } from "../extensions/plugin-guides.js";
|
|
3
|
-
import { ExtensionRegistry } from "../extensions/registry.js";
|
|
4
3
|
import type { WriteActor } from "../identity/write-actor.js";
|
|
5
4
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
6
5
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
@@ -259,11 +258,6 @@ export interface OAuthSetup {
|
|
|
259
258
|
export declare class ConnectorsService {
|
|
260
259
|
private readonly kdb;
|
|
261
260
|
private readonly registry;
|
|
262
|
-
/**
|
|
263
|
-
* Extension manifests: a derived default slug avoids an extension's name (an explicit slug may
|
|
264
|
-
* take one; connector and extension tool keys live in separate namespaces).
|
|
265
|
-
*/
|
|
266
|
-
private readonly extensions;
|
|
267
261
|
private readonly credentials;
|
|
268
262
|
private readonly config;
|
|
269
263
|
private readonly drift;
|
|
@@ -276,12 +270,7 @@ export declare class ConnectorsService {
|
|
|
276
270
|
readonly txHost: TransactionHost;
|
|
277
271
|
/** Setup guides of connector templates (spec 2026-11-09-setup-guides-v2-design.md §11). */
|
|
278
272
|
private readonly guides;
|
|
279
|
-
constructor(kdb: KernelDb, registry: ToolRegistry,
|
|
280
|
-
/**
|
|
281
|
-
* Extension manifests: a derived default slug avoids an extension's name (an explicit slug may
|
|
282
|
-
* take one; connector and extension tool keys live in separate namespaces).
|
|
283
|
-
*/
|
|
284
|
-
extensions: Pick<ExtensionRegistry, "manifests">, credentials: Credentials, config: Pick<KernelConfigService, "get">, drift: ToolDrift,
|
|
273
|
+
constructor(kdb: KernelDb, registry: ToolRegistry, credentials: Credentials, config: Pick<KernelConfigService, "get">, drift: ToolDrift,
|
|
285
274
|
/** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
|
|
286
275
|
orgLock: Pick<OrgLock, "lock" | "tryLock" | "lockAndAuthorize">,
|
|
287
276
|
/** Owed work: the tool listings that follow a deferred create or a setup (§5.2.1). */
|
|
@@ -300,7 +289,9 @@ export declare class ConnectorsService {
|
|
|
300
289
|
private slugTaken;
|
|
301
290
|
/**
|
|
302
291
|
* The first free default name and slug ({@link CreateConnectorInput.slugDefault}), read in the
|
|
303
|
-
* caller's transaction under the org lock.
|
|
292
|
+
* caller's transaction under the org lock. Only live connectors hold a slug: an extension's name
|
|
293
|
+
* does not, as for an explicit slug, since connector tools are `mcp__<slug>__<tool>` and extension
|
|
294
|
+
* tools `plugin__<plugin>__<tool>` (a template's default slug is often its plugin's name: slack).
|
|
304
295
|
*/
|
|
305
296
|
private freeName;
|
|
306
297
|
private resolveSlug;
|
|
@@ -17,7 +17,6 @@ import { assertItemInstallable } from "../catalog/request-rules.js";
|
|
|
17
17
|
import { productName } from "../config/product-name.js";
|
|
18
18
|
import { ApiError } from "../errors.js";
|
|
19
19
|
import { PluginGuides, } from "../extensions/plugin-guides.js";
|
|
20
|
-
import { ExtensionRegistry } from "../extensions/registry.js";
|
|
21
20
|
import { audit } from "../identity/audit.js";
|
|
22
21
|
import { assertCustomAllowed } from "../identity/settings.js";
|
|
23
22
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
@@ -116,7 +115,6 @@ const unannotated = (a) => a?.readOnlyHint === undefined && a?.destructiveHint =
|
|
|
116
115
|
let ConnectorsService = class ConnectorsService {
|
|
117
116
|
kdb;
|
|
118
117
|
registry;
|
|
119
|
-
extensions;
|
|
120
118
|
credentials;
|
|
121
119
|
config;
|
|
122
120
|
drift;
|
|
@@ -125,12 +123,7 @@ let ConnectorsService = class ConnectorsService {
|
|
|
125
123
|
locks;
|
|
126
124
|
txHost;
|
|
127
125
|
guides;
|
|
128
|
-
constructor(kdb, registry,
|
|
129
|
-
/**
|
|
130
|
-
* Extension manifests: a derived default slug avoids an extension's name (an explicit slug may
|
|
131
|
-
* take one; connector and extension tool keys live in separate namespaces).
|
|
132
|
-
*/
|
|
133
|
-
extensions, credentials, config, drift,
|
|
126
|
+
constructor(kdb, registry, credentials, config, drift,
|
|
134
127
|
/** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
|
|
135
128
|
orgLock,
|
|
136
129
|
/** Owed work: the tool listings that follow a deferred create or a setup (§5.2.1). */
|
|
@@ -141,7 +134,6 @@ let ConnectorsService = class ConnectorsService {
|
|
|
141
134
|
guides) {
|
|
142
135
|
this.kdb = kdb;
|
|
143
136
|
this.registry = registry;
|
|
144
|
-
this.extensions = extensions;
|
|
145
137
|
this.credentials = credentials;
|
|
146
138
|
this.config = config;
|
|
147
139
|
this.drift = drift;
|
|
@@ -189,7 +181,9 @@ let ConnectorsService = class ConnectorsService {
|
|
|
189
181
|
}
|
|
190
182
|
/**
|
|
191
183
|
* The first free default name and slug ({@link CreateConnectorInput.slugDefault}), read in the
|
|
192
|
-
* caller's transaction under the org lock.
|
|
184
|
+
* caller's transaction under the org lock. Only live connectors hold a slug: an extension's name
|
|
185
|
+
* does not, as for an explicit slug, since connector tools are `mcp__<slug>__<tool>` and extension
|
|
186
|
+
* tools `plugin__<plugin>__<tool>` (a template's default slug is often its plugin's name: slack).
|
|
193
187
|
*/
|
|
194
188
|
async freeName(t, orgId, d) {
|
|
195
189
|
const base = deriveSlug(d.name ?? d.display) ?? deriveSlug(d.display);
|
|
@@ -202,8 +196,6 @@ let ConnectorsService = class ConnectorsService {
|
|
|
202
196
|
.where("org_id", "=", orgId)
|
|
203
197
|
.where("deleted_at", "is", null)
|
|
204
198
|
.execute()).map((r) => r.slug));
|
|
205
|
-
for (const m of this.extensions.manifests())
|
|
206
|
-
taken.add(m.name);
|
|
207
199
|
for (let n = 1;; n++) {
|
|
208
200
|
const suffix = n === 1 ? "" : `_${n}`;
|
|
209
201
|
const slug = `${base.slice(0, 24 - suffix.length).replace(/_+$/, "")}${suffix}`;
|
|
@@ -1987,14 +1979,13 @@ ConnectorsService = __decorate([
|
|
|
1987
1979
|
Injectable(),
|
|
1988
1980
|
__param(0, Inject(KernelDb)),
|
|
1989
1981
|
__param(1, Inject(ToolRegistry)),
|
|
1990
|
-
__param(2, Inject(
|
|
1991
|
-
__param(3, Inject(
|
|
1992
|
-
__param(4, Inject(
|
|
1993
|
-
__param(5, Inject(
|
|
1994
|
-
__param(6, Inject(
|
|
1995
|
-
__param(7, Inject(
|
|
1996
|
-
__param(8, Inject(
|
|
1997
|
-
__param(9, Inject(
|
|
1998
|
-
__param(10, Inject(PluginGuides))
|
|
1982
|
+
__param(2, Inject(Credentials)),
|
|
1983
|
+
__param(3, Inject(KernelConfigService)),
|
|
1984
|
+
__param(4, Inject(ToolDrift)),
|
|
1985
|
+
__param(5, Inject(OrgLock)),
|
|
1986
|
+
__param(6, Inject(OutboxService)),
|
|
1987
|
+
__param(7, Inject(LockService)),
|
|
1988
|
+
__param(8, Inject(TransactionHost)),
|
|
1989
|
+
__param(9, Inject(PluginGuides))
|
|
1999
1990
|
], ConnectorsService);
|
|
2000
1991
|
export { ConnectorsService };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Logger } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import type { Redis } from "ioredis";
|
|
3
|
+
import { type HandoffRecord } from "../../handoff/handoff-paths.js";
|
|
3
4
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
4
5
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
5
6
|
import { TransactionHost } from "../../infra/database/transaction-host.js";
|
|
@@ -36,8 +37,21 @@ export declare class OAuthFlow {
|
|
|
36
37
|
/** 某成员首次成功连接、而连接器还没有工具快照时(A16:用该成员的 token 刷新工具清单)。 */
|
|
37
38
|
private onFirstConnect;
|
|
38
39
|
private app;
|
|
39
|
-
/**
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Every check {@link start} makes before it writes the state: `http.publicUrl` is set, the
|
|
42
|
+
* connector exists, is an active `oauth_code` connector and has a client with an authorization
|
|
43
|
+
* endpoint (a dcr connector created without a client registers it now, once across replicas,
|
|
44
|
+
* R16). A browser handoff start runs it so a connector that cannot be connected fails in the app,
|
|
45
|
+
* not after the person signed in in the browser.
|
|
46
|
+
*/
|
|
47
|
+
check(orgId: string, connectorId: string, accountId: string): Promise<void>;
|
|
48
|
+
private prepare;
|
|
49
|
+
/**
|
|
50
|
+
* Builds the authorization URL. `binding` is this browser's binding value (the controller writes
|
|
51
|
+
* the cookie). With `handoff` (a browser handoff redeemed in this browser), the state's return
|
|
52
|
+
* page is the handoff done page built from the ticket's record, and `returnTo` is ignored.
|
|
53
|
+
*/
|
|
54
|
+
start(orgId: string, connectorId: string, accountId: string, returnTo: string | undefined, binding: string, handoff?: HandoffRecord): Promise<{
|
|
41
55
|
authorizationUrl: string;
|
|
42
56
|
}>;
|
|
43
57
|
/** 回跳地址:`http.publicUrl` + 站内路径 + 结果参数(未配置 publicUrl 时为相对地址)。 */
|
package/dist/tools/oauth/flow.js
CHANGED
|
@@ -10,6 +10,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
10
10
|
import { createHash, randomBytes } from "node:crypto";
|
|
11
11
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
12
|
import { ApiError } from "../../errors.js";
|
|
13
|
+
import { handoffDonePath } from "../../handoff/handoff-paths.js";
|
|
13
14
|
import { audit } from "../../identity/audit.js";
|
|
14
15
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
15
16
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
@@ -77,8 +78,17 @@ let OAuthFlow = class OAuthFlow {
|
|
|
77
78
|
.where("deleted_at", "is", null)
|
|
78
79
|
.executeTakeFirst();
|
|
79
80
|
}
|
|
80
|
-
/**
|
|
81
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Every check {@link start} makes before it writes the state: `http.publicUrl` is set, the
|
|
83
|
+
* connector exists, is an active `oauth_code` connector and has a client with an authorization
|
|
84
|
+
* endpoint (a dcr connector created without a client registers it now, once across replicas,
|
|
85
|
+
* R16). A browser handoff start runs it so a connector that cannot be connected fails in the app,
|
|
86
|
+
* not after the person signed in in the browser.
|
|
87
|
+
*/
|
|
88
|
+
async check(orgId, connectorId, accountId) {
|
|
89
|
+
await this.prepare(orgId, connectorId, accountId);
|
|
90
|
+
}
|
|
91
|
+
async prepare(orgId, connectorId, accountId) {
|
|
82
92
|
const publicUrl = this.config.get().http.publicUrl;
|
|
83
93
|
if (!publicUrl)
|
|
84
94
|
throw publicUrlRequired();
|
|
@@ -103,6 +113,15 @@ let OAuthFlow = class OAuthFlow {
|
|
|
103
113
|
if (!client?.authorizationEndpoint) {
|
|
104
114
|
throw new ApiError("connector.connect_failed", "This connector needs to be configured by an organization admin before members can connect it.");
|
|
105
115
|
}
|
|
116
|
+
return { publicUrl, app, client, authorizationEndpoint: client.authorizationEndpoint };
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Builds the authorization URL. `binding` is this browser's binding value (the controller writes
|
|
120
|
+
* the cookie). With `handoff` (a browser handoff redeemed in this browser), the state's return
|
|
121
|
+
* page is the handoff done page built from the ticket's record, and `returnTo` is ignored.
|
|
122
|
+
*/
|
|
123
|
+
async start(orgId, connectorId, accountId, returnTo, binding, handoff) {
|
|
124
|
+
const { publicUrl, app, client, authorizationEndpoint } = await this.prepare(orgId, connectorId, accountId);
|
|
106
125
|
const state = b64url();
|
|
107
126
|
const verifier = b64url();
|
|
108
127
|
const redirectUri = callbackUrl(publicUrl);
|
|
@@ -112,12 +131,12 @@ let OAuthFlow = class OAuthFlow {
|
|
|
112
131
|
accountId,
|
|
113
132
|
verifier,
|
|
114
133
|
redirectUri,
|
|
115
|
-
returnTo: returnTo ?? DEFAULT_RETURN_PATH,
|
|
134
|
+
returnTo: handoff !== undefined ? handoffDonePath(handoff) : (returnTo ?? DEFAULT_RETURN_PATH),
|
|
116
135
|
binding: sha256(binding),
|
|
117
136
|
...(client.issuer !== undefined && { issuer: client.issuer }),
|
|
118
137
|
};
|
|
119
138
|
await this.redis.set(stateKey(state), JSON.stringify(rec), "EX", OAUTH_STATE_TTL_SECONDS);
|
|
120
|
-
const url = new URL(
|
|
139
|
+
const url = new URL(authorizationEndpoint);
|
|
121
140
|
url.searchParams.set("response_type", "code");
|
|
122
141
|
url.searchParams.set("client_id", client.clientId);
|
|
123
142
|
url.searchParams.set("redirect_uri", redirectUri);
|
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
2
2
|
import type { z } from "zod";
|
|
3
|
+
import { HandoffTickets } from "../../handoff/handoff-tickets.js";
|
|
3
4
|
import { type Principal } from "../../identity/principal.js";
|
|
4
5
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
5
6
|
import { type OrgContext } from "../../orgs/org.guard.js";
|
|
6
7
|
import { OAuthFlow } from "./flow.js";
|
|
7
|
-
import { OAuthConnectBodySchema } from "./oauth.schemas.js";
|
|
8
|
+
import { OAuthConnectBodySchema, type OAuthConnectResponse } from "./oauth.schemas.js";
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* Writes the OAuth browser binding cookie `cap_oauth` (HttpOnly, SameSite=Lax: the authorization
|
|
11
|
+
* server's redirect back is a cross-site top-level GET, which Lax sends; Path only the callback).
|
|
12
|
+
*/
|
|
13
|
+
export declare function setOAuthBindingCookie(reply: FastifyReply, binding: string, config: {
|
|
14
|
+
auth: {
|
|
15
|
+
cookieSecure: boolean;
|
|
16
|
+
};
|
|
17
|
+
}): void;
|
|
18
|
+
/**
|
|
19
|
+
* Starts an OAuth connection (`POST /orgs/:orgId/connectors/:connectorId/connect`,
|
|
20
|
+
* `connectors.use`, user principals only): returns the authorization URL and writes the browser
|
|
21
|
+
* binding cookie ({@link setOAuthBindingCookie}). With `handoff` (a native shell, shell external
|
|
22
|
+
* authorization spec §1): the same checks, then a handoff ticket and its web page instead, with
|
|
23
|
+
* no cookie and no OAuth state; the flow starts when the person redeems it in the system browser.
|
|
11
24
|
*/
|
|
12
25
|
export declare class OAuthConnectController {
|
|
13
26
|
private readonly flow;
|
|
14
27
|
private readonly config;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}>;
|
|
28
|
+
private readonly tickets;
|
|
29
|
+
constructor(flow: OAuthFlow, config: KernelConfigService, tickets: HandoffTickets);
|
|
30
|
+
connect(principal: Principal, org: OrgContext, connectorId: string, body: z.infer<typeof OAuthConnectBodySchema>, req: FastifyRequest, reply: FastifyReply): Promise<OAuthConnectResponse>;
|
|
19
31
|
}
|
|
20
32
|
/**
|
|
21
33
|
* 授权服务器的回跳(`GET /api/v1/oauth/callback`,公开路由:浏览器从授权服务器跳回来,会话 Cookie 可能已过期;
|
|
@@ -8,39 +8,68 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
10
|
import { Controller, Get, HttpCode, Inject, Param, Post, Query, Req, Res } from "@nestjs/common";
|
|
11
|
+
import { assertReturnSchemeAllowed } from "../../handoff/app-schemes.js";
|
|
12
|
+
import { HandoffTickets } from "../../handoff/handoff-tickets.js";
|
|
11
13
|
import { CurrentPrincipal, Public, requireUser } from "../../identity/principal.js";
|
|
12
14
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
13
15
|
import { createZodDto } from "../../infra/validation/create-zod-dto.js";
|
|
14
16
|
import { ZBody } from "../../infra/validation/zod-params.js";
|
|
15
17
|
import { CurrentOrg, OrgAction } from "../../orgs/org.guard.js";
|
|
16
|
-
import { browserBinding, OAUTH_BINDING_COOKIE, OAUTH_CALLBACK_PATH, OAUTH_STATE_TTL_SECONDS, OAuthFlow, } from "./flow.js";
|
|
18
|
+
import { browserBinding, DEFAULT_RETURN_PATH, OAUTH_BINDING_COOKIE, OAUTH_CALLBACK_PATH, OAUTH_STATE_TTL_SECONDS, OAuthFlow, } from "./flow.js";
|
|
17
19
|
import { OAuthConnectBodySchema } from "./oauth.schemas.js";
|
|
18
20
|
class OAuthConnectBody extends createZodDto(OAuthConnectBodySchema, "OAuthConnectBody") {
|
|
19
21
|
}
|
|
20
22
|
/** 查询参数只取单值字符串(重复参数 / 数组按缺失处理)。 */
|
|
21
23
|
const one = (v) => typeof v === "string" && v.length <= 4096 ? v : undefined;
|
|
22
24
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
+
* Writes the OAuth browser binding cookie `cap_oauth` (HttpOnly, SameSite=Lax: the authorization
|
|
26
|
+
* server's redirect back is a cross-site top-level GET, which Lax sends; Path only the callback).
|
|
27
|
+
*/
|
|
28
|
+
export function setOAuthBindingCookie(reply, binding, config) {
|
|
29
|
+
reply.setCookie(OAUTH_BINDING_COOKIE, binding, {
|
|
30
|
+
path: OAUTH_CALLBACK_PATH,
|
|
31
|
+
httpOnly: true,
|
|
32
|
+
sameSite: "lax",
|
|
33
|
+
secure: config.auth.cookieSecure,
|
|
34
|
+
maxAge: OAUTH_STATE_TTL_SECONDS,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Starts an OAuth connection (`POST /orgs/:orgId/connectors/:connectorId/connect`,
|
|
39
|
+
* `connectors.use`, user principals only): returns the authorization URL and writes the browser
|
|
40
|
+
* binding cookie ({@link setOAuthBindingCookie}). With `handoff` (a native shell, shell external
|
|
41
|
+
* authorization spec §1): the same checks, then a handoff ticket and its web page instead, with
|
|
42
|
+
* no cookie and no OAuth state; the flow starts when the person redeems it in the system browser.
|
|
25
43
|
*/
|
|
26
44
|
let OAuthConnectController = class OAuthConnectController {
|
|
27
45
|
flow;
|
|
28
46
|
config;
|
|
29
|
-
|
|
47
|
+
tickets;
|
|
48
|
+
constructor(flow, config, tickets) {
|
|
30
49
|
this.flow = flow;
|
|
31
50
|
this.config = config;
|
|
51
|
+
this.tickets = tickets;
|
|
32
52
|
}
|
|
33
53
|
async connect(principal, org, connectorId, body, req, reply) {
|
|
34
54
|
const p = requireUser(principal);
|
|
55
|
+
if (body.handoff !== undefined) {
|
|
56
|
+
const returnScheme = body.handoff.returnScheme;
|
|
57
|
+
assertReturnSchemeAllowed(this.config.get(), returnScheme);
|
|
58
|
+
await this.flow.check(org.orgId, connectorId, p.account.id);
|
|
59
|
+
const ticket = await this.tickets.create({
|
|
60
|
+
kind: "connector",
|
|
61
|
+
accountId: p.account.id,
|
|
62
|
+
orgId: org.orgId,
|
|
63
|
+
connectorId,
|
|
64
|
+
returnTo: body.returnTo ?? DEFAULT_RETURN_PATH,
|
|
65
|
+
...(returnScheme !== undefined && { returnScheme }),
|
|
66
|
+
});
|
|
67
|
+
reply.header("cache-control", "no-store");
|
|
68
|
+
return { handoffUrl: this.tickets.url(ticket) };
|
|
69
|
+
}
|
|
35
70
|
const binding = browserBinding(req.cookies[OAUTH_BINDING_COOKIE]);
|
|
36
71
|
const out = await this.flow.start(org.orgId, connectorId, p.account.id, body.returnTo, binding);
|
|
37
|
-
reply
|
|
38
|
-
path: OAUTH_CALLBACK_PATH,
|
|
39
|
-
httpOnly: true,
|
|
40
|
-
sameSite: "lax",
|
|
41
|
-
secure: this.config.get().auth.cookieSecure,
|
|
42
|
-
maxAge: OAUTH_STATE_TTL_SECONDS,
|
|
43
|
-
});
|
|
72
|
+
setOAuthBindingCookie(reply, binding, this.config.get());
|
|
44
73
|
return out;
|
|
45
74
|
}
|
|
46
75
|
};
|
|
@@ -58,7 +87,8 @@ __decorate([
|
|
|
58
87
|
OAuthConnectController = __decorate([
|
|
59
88
|
Controller("api/v1/orgs/:orgId/connectors"),
|
|
60
89
|
__param(0, Inject(OAuthFlow)),
|
|
61
|
-
__param(1, Inject(KernelConfigService))
|
|
90
|
+
__param(1, Inject(KernelConfigService)),
|
|
91
|
+
__param(2, Inject(HandoffTickets))
|
|
62
92
|
], OAuthConnectController);
|
|
63
93
|
export { OAuthConnectController };
|
|
64
94
|
/**
|
|
@@ -2,11 +2,24 @@ import { z } from "zod";
|
|
|
2
2
|
import type { RouteSchemas } from "../../platform/route-schemas.js";
|
|
3
3
|
/**
|
|
4
4
|
* `POST …/connectors/:connectorId/connect`: where the browser returns after the authorization (a
|
|
5
|
-
* relative path on this site; default the connections page).
|
|
5
|
+
* relative path on this site; default the connections page). `handoff` (a native shell): finish
|
|
6
|
+
* the authorization in the system browser through a handoff ticket instead.
|
|
6
7
|
*/
|
|
7
8
|
export declare const OAuthConnectBodySchema: z.ZodObject<{
|
|
8
9
|
returnTo: z.ZodOptional<z.ZodString>;
|
|
10
|
+
handoff: z.ZodOptional<z.ZodObject<{
|
|
11
|
+
returnScheme: z.ZodOptional<z.ZodString>;
|
|
12
|
+
}, z.core.$strict>>;
|
|
9
13
|
}, z.core.$strict>;
|
|
14
|
+
/**
|
|
15
|
+
* The connect route's answer: the authorization URL (the binding cookie is set on this browser),
|
|
16
|
+
* or with `handoff` the web page that starts it in the system browser (no cookie, no state).
|
|
17
|
+
*/
|
|
18
|
+
export type OAuthConnectResponse = {
|
|
19
|
+
authorizationUrl: string;
|
|
20
|
+
} | {
|
|
21
|
+
handoffUrl: string;
|
|
22
|
+
};
|
|
10
23
|
/**
|
|
11
24
|
* The connect route's schemas for the Platform parity check (`connectors.connect` mirrors it; the
|
|
12
25
|
* OAuth callback stays an exclusion).
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { HandoffRequestSchema } from "../../handoff/app-schemes.js";
|
|
2
3
|
import { ConnectorPathParams } from "../connectors.schemas.js";
|
|
3
4
|
import { isSafeReturnPath } from "./flow-paths.js";
|
|
4
5
|
/**
|
|
5
6
|
* `POST …/connectors/:connectorId/connect`: where the browser returns after the authorization (a
|
|
6
|
-
* relative path on this site; default the connections page).
|
|
7
|
+
* relative path on this site; default the connections page). `handoff` (a native shell): finish
|
|
8
|
+
* the authorization in the system browser through a handoff ticket instead.
|
|
7
9
|
*/
|
|
8
10
|
export const OAuthConnectBodySchema = z
|
|
9
11
|
.object({
|
|
@@ -12,6 +14,7 @@ export const OAuthConnectBodySchema = z
|
|
|
12
14
|
.max(500)
|
|
13
15
|
.refine(isSafeReturnPath, { message: "returnTo must be a relative path on this site" })
|
|
14
16
|
.optional(),
|
|
17
|
+
handoff: HandoffRequestSchema.optional(),
|
|
15
18
|
})
|
|
16
19
|
.strict();
|
|
17
20
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/kernel",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.105",
|
|
4
4
|
"description": "Capybara kernel: config, database, plugin host and HTTP API of the Capybara agent platform",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"yaml": "^2.9.1",
|
|
67
67
|
"yauzl": "3.4.0",
|
|
68
68
|
"zod": "^4.6.7",
|
|
69
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
69
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.84"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@langchain/langgraph-checkpoint-validation": "^1.2.0",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"@types/ws": "^8.18.2",
|
|
76
76
|
"@types/yauzl": "3.4.0",
|
|
77
77
|
"ws": "^8.22.0",
|
|
78
|
-
"@capybara-hq/testing": "0.1.0-rc.
|
|
78
|
+
"@capybara-hq/testing": "0.1.0-rc.46"
|
|
79
79
|
},
|
|
80
80
|
"scripts": {
|
|
81
81
|
"build": "tsc -b"
|