@agent-native/core 0.209.1 → 0.209.2-nightly-20261015213030
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/agent/actions/list-model-providers.js +15 -2
- package/dist/agent/app-model-defaults.d.ts +9 -13
- package/dist/agent/app-model-defaults.js +23 -5
- package/dist/agent/default-agent-engine.d.ts +5 -3
- package/dist/agent/default-agent-engine.js +38 -8
- package/dist/agent/durable-background.d.ts +2 -0
- package/dist/agent/durable-background.js +2 -0
- package/dist/agent/engine/registry.js +6 -0
- package/dist/agent/production-agent.js +58 -5
- package/dist/automations/service.d.ts +2 -0
- package/dist/automations/service.js +4 -1
- package/dist/client/analytics.js +48 -1
- package/dist/client/application-state.js +7 -2
- package/dist/client/chat/agentkit-protocol.js +12 -3
- package/dist/client/client-bootstrap.js +2 -0
- package/dist/client/problem-report.d.ts +11 -0
- package/dist/client/problem-report.js +19 -0
- package/dist/client/recorder.d.ts +88 -0
- package/dist/client/recorder.js +585 -0
- package/dist/client/route-chunk-recovery.js +4 -1
- package/dist/client/sign-out.js +2 -0
- package/dist/client/use-action.js +3 -0
- package/dist/collab/routes.d.ts +1 -1
- package/dist/collab/struct-routes.d.ts +1 -1
- package/dist/file-upload/actions/upload-image.d.ts +2 -2
- package/dist/integrations/google-docs-poller.js +12 -5
- package/dist/integrations/plugin.js +86 -19
- package/dist/integrations/request-scope.d.ts +16 -0
- package/dist/integrations/request-scope.js +27 -0
- package/dist/integrations/webhook-handler.d.ts +2 -0
- package/dist/integrations/webhook-handler.js +38 -18
- package/dist/jobs/background-automation-runner.d.ts +2 -0
- package/dist/jobs/background-automation-runner.js +25 -2
- package/dist/jobs/scheduler.js +36 -22
- package/dist/mcp/screen-memory-stdio.d.ts +7 -7
- package/dist/org/context.d.ts +12 -0
- package/dist/org/context.js +26 -11
- package/dist/org/index.d.ts +1 -1
- package/dist/problem-report/actions/read-problem-report.d.ts +27 -0
- package/dist/problem-report/actions/read-problem-report.js +103 -0
- package/dist/problem-report/actions/save-problem-report.d.ts +35 -0
- package/dist/problem-report/actions/save-problem-report.js +36 -0
- package/dist/problem-report/format.d.ts +7 -0
- package/dist/problem-report/format.js +110 -0
- package/dist/problem-report/schema.d.ts +41 -0
- package/dist/problem-report/schema.js +34 -0
- package/dist/progress/routes.d.ts +1 -1
- package/dist/provider-api/actions/custom-provider-registration.d.ts +8 -8
- package/dist/provider-api/actions/provider-api.d.ts +14 -14
- package/dist/provider-api/corpus-jobs.d.ts +2 -2
- package/dist/resources/handlers.d.ts +1 -1
- package/dist/scripts/agent-engines/manage-agent-engine.js +9 -4
- package/dist/scripts/agent-engines/set-agent-engine.js +7 -1
- package/dist/server/action-discovery.js +11 -0
- package/dist/server/action-routes.js +2 -0
- package/dist/server/agent-chat-ai-setup.d.ts +2 -3
- package/dist/server/agent-chat-ai-setup.js +18 -4
- package/dist/server/agent-chat-plugin.js +97 -22
- package/dist/server/agent-chat-stream.d.ts +17 -5
- package/dist/server/agent-chat-stream.js +15 -0
- package/dist/server/agent-engine-api-key-route.d.ts +1 -1
- package/dist/server/agent-engine-default-model-route.d.ts +2 -2
- package/dist/server/agent-engine-default-model-route.js +39 -6
- package/dist/server/agent-engine-ollama-models-route.d.ts +2 -2
- package/dist/server/agent-engine-status-cache.d.ts +4 -0
- package/dist/server/agent-engine-status-cache.js +2 -0
- package/dist/server/agent-run-context.d.ts +9 -0
- package/dist/server/agent-run-context.js +82 -17
- package/dist/server/builder-browser.js +18 -3
- package/dist/server/chatgpt-subscription-oauth.js +12 -17
- package/dist/server/core-routes-plugin.d.ts +1 -0
- package/dist/server/core-routes-plugin.js +46 -9
- package/dist/server/loopback.d.ts +1 -0
- package/dist/server/loopback.js +30 -0
- package/dist/server/origin-allowlist.d.ts +2 -0
- package/dist/server/origin-allowlist.js +13 -1
- package/dist/server/realtime-token.d.ts +1 -1
- package/dist/server/request-context.d.ts +4 -0
- package/dist/templates/chat/app/components/layout/Sidebar.tsx +11 -1
- package/dist/templates/factory/.agents/skills/review-latest-feedback/SKILL.md +2 -2
- package/dist/templates/factory/.agents/skills/review-latest-feedback/references/channel-inventory.md +31 -0
- package/dist/triggers/dispatcher.js +117 -31
- package/docs/content/recorder.mdx +259 -0
- package/docs/content/template-clips-developers.mdx +9 -8
- package/docs/content/template-clips-features.mdx +8 -7
- package/package.json +5 -3
- package/src/templates/chat/app/components/layout/Sidebar.tsx +11 -1
- package/src/templates/factory/.agents/skills/review-latest-feedback/SKILL.md +2 -2
- package/src/templates/factory/.agents/skills/review-latest-feedback/references/channel-inventory.md +31 -0
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
export declare const AGENT_CHAT_STREAM_PATH = "/_agent-native/agent-chat-stream";
|
|
2
2
|
export declare const AGENT_CHAT_STREAM_TOKEN_SUFFIX = "/stream-token";
|
|
3
3
|
export declare const AGENT_CHAT_STREAM_TOKEN_TTL_SECONDS: number;
|
|
4
|
-
export
|
|
4
|
+
export type AgentChatStreamPrincipal = {
|
|
5
5
|
ownerEmail: string;
|
|
6
|
-
orgId: string | null;
|
|
7
6
|
authUserId?: string;
|
|
8
|
-
}
|
|
7
|
+
} & ({
|
|
8
|
+
orgId: string;
|
|
9
|
+
orgScope?: never;
|
|
10
|
+
} | {
|
|
11
|
+
orgId: null;
|
|
12
|
+
orgScope: "personal";
|
|
13
|
+
});
|
|
14
|
+
type AgentChatStreamTokenScope = {
|
|
15
|
+
orgId: string;
|
|
16
|
+
orgScope?: never;
|
|
17
|
+
} | {
|
|
18
|
+
orgId: null;
|
|
19
|
+
orgScope: "personal";
|
|
20
|
+
};
|
|
9
21
|
export declare function isAgentChatStreamingRuntime(): boolean;
|
|
10
22
|
export declare function readAgentChatStreamBearerToken(authorization: string | undefined): string | null;
|
|
11
23
|
export declare function createAgentChatStreamToken(input: {
|
|
12
24
|
ownerEmail: string;
|
|
13
|
-
orgId?: string | null;
|
|
14
25
|
authUserId?: string;
|
|
15
|
-
}): Promise<string>;
|
|
26
|
+
} & AgentChatStreamTokenScope): Promise<string>;
|
|
16
27
|
export declare function verifyAgentChatStreamToken(token: string): Promise<AgentChatStreamPrincipal | null>;
|
|
28
|
+
export {};
|
|
@@ -41,6 +41,13 @@ export function readAgentChatStreamBearerToken(authorization) {
|
|
|
41
41
|
}
|
|
42
42
|
export async function createAgentChatStreamToken(input) {
|
|
43
43
|
const ownerEmail = validateOwnerEmail(input.ownerEmail);
|
|
44
|
+
if (input.orgScope === "personal" && input.orgId !== null) {
|
|
45
|
+
throw new Error("Personal agent-chat stream scope cannot include an org.");
|
|
46
|
+
}
|
|
47
|
+
if (input.orgScope !== "personal" &&
|
|
48
|
+
(typeof input.orgId !== "string" || !input.orgId.trim())) {
|
|
49
|
+
throw new Error("Agent-chat stream tokens require a resolved organization or personal scope.");
|
|
50
|
+
}
|
|
44
51
|
const authUserId = input.authUserId?.trim();
|
|
45
52
|
if (input.authUserId !== undefined &&
|
|
46
53
|
(!authUserId ||
|
|
@@ -53,6 +60,7 @@ export async function createAgentChatStreamToken(input) {
|
|
|
53
60
|
token_type: AGENT_CHAT_STREAM_TOKEN_TYPE,
|
|
54
61
|
sub: ownerEmail,
|
|
55
62
|
org_id: input.orgId ?? null,
|
|
63
|
+
...(input.orgScope === "personal" ? { org_scope: "personal" } : {}),
|
|
56
64
|
...(authUserId ? { auth_user_id: authUserId } : {}),
|
|
57
65
|
})
|
|
58
66
|
.setProtectedHeader({ alg: "HS256", typ: "JWT" })
|
|
@@ -80,6 +88,12 @@ export async function verifyAgentChatStreamToken(token) {
|
|
|
80
88
|
const orgId = payload.org_id;
|
|
81
89
|
if (orgId !== null && typeof orgId !== "string")
|
|
82
90
|
return null;
|
|
91
|
+
const orgScope = payload.org_scope;
|
|
92
|
+
if ((orgScope !== undefined && orgScope !== "personal") ||
|
|
93
|
+
(orgScope === "personal" && orgId !== null) ||
|
|
94
|
+
(orgId === null && orgScope !== "personal")) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
83
97
|
const authUserId = payload.auth_user_id;
|
|
84
98
|
if (authUserId !== undefined &&
|
|
85
99
|
(typeof authUserId !== "string" ||
|
|
@@ -91,6 +105,7 @@ export async function verifyAgentChatStreamToken(token) {
|
|
|
91
105
|
return {
|
|
92
106
|
ownerEmail: validateOwnerEmail(payload.sub),
|
|
93
107
|
orgId,
|
|
108
|
+
...(orgScope === "personal" ? { orgScope: "personal" } : {}),
|
|
94
109
|
...(authUserId ? { authUserId } : {}),
|
|
95
110
|
};
|
|
96
111
|
// coercion-ok: signature, issuer, audience, and expiry failures mean invalid authorization.
|
|
@@ -66,6 +66,7 @@ export declare function createAgentEngineApiKeyHandler(): import("h3").EventHand
|
|
|
66
66
|
error: any;
|
|
67
67
|
code: any;
|
|
68
68
|
} | {
|
|
69
|
+
error?: undefined;
|
|
69
70
|
errorCode?: undefined;
|
|
70
71
|
code?: undefined;
|
|
71
72
|
ok: boolean;
|
|
@@ -73,6 +74,5 @@ export declare function createAgentEngineApiKeyHandler(): import("h3").EventHand
|
|
|
73
74
|
baseUrlKey?: string;
|
|
74
75
|
scope: AgentEngineApiKeyScope;
|
|
75
76
|
defaultModel?: import("./agent-engine-default-model-route.js").SavedKeyDefaultModelOutcome;
|
|
76
|
-
error?: undefined;
|
|
77
77
|
}>>;
|
|
78
78
|
export {};
|
|
@@ -9,16 +9,16 @@ export declare const AGENT_ENGINE_API_KEY_ACTION_NAME = "agent-engine-api-key";
|
|
|
9
9
|
*/
|
|
10
10
|
export declare function createAgentEngineDisconnectHandler(): import("h3").EventHandlerWithFetch<import("h3").EventHandlerRequest, Promise<{
|
|
11
11
|
error: string;
|
|
12
|
-
ok?: undefined;
|
|
13
12
|
scope?: undefined;
|
|
13
|
+
ok?: undefined;
|
|
14
14
|
} | {
|
|
15
15
|
ok: boolean;
|
|
16
16
|
error: any;
|
|
17
17
|
scope?: undefined;
|
|
18
18
|
} | {
|
|
19
|
-
error?: undefined;
|
|
20
19
|
ok: boolean;
|
|
21
20
|
scope: "org" | "user";
|
|
21
|
+
error?: undefined;
|
|
22
22
|
}>>;
|
|
23
23
|
export interface DefaultModelSelectionRequest {
|
|
24
24
|
engine: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineEventHandler, getMethod, setResponseStatus, } from "h3";
|
|
2
2
|
import { clearDefaultAgentEngineSelection, recordDefaultAgentEngineRefusal, resolveDefaultAgentEngineAuthority, } from "../agent/default-agent-engine.js";
|
|
3
|
-
import {
|
|
3
|
+
import { resolveAgentRunOrgScope } from "./agent-run-context.js";
|
|
4
4
|
import { getSession } from "./auth.js";
|
|
5
5
|
import { runWithRequestContext } from "./request-context.js";
|
|
6
6
|
export const AGENT_ENGINE_DISCONNECT_ACTION_NAME = "agent-engine-disconnect";
|
|
@@ -26,8 +26,24 @@ export function createAgentEngineDisconnectHandler() {
|
|
|
26
26
|
// Not caught separately: an unreadable org context must not downgrade an
|
|
27
27
|
// org member's clear to a personal one that answers "ok" and changes
|
|
28
28
|
// nothing.
|
|
29
|
-
const
|
|
30
|
-
|
|
29
|
+
const orgScope = await resolveAgentRunOrgScope({
|
|
30
|
+
event,
|
|
31
|
+
ownerContext: { owner: session.email, anonymous: false },
|
|
32
|
+
});
|
|
33
|
+
if (!orgScope.orgId && orgScope.orgScope !== "personal") {
|
|
34
|
+
setResponseStatus(event, 503);
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
error: "Organization scope is temporarily unavailable.",
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const ctx = {
|
|
41
|
+
userEmail: session.email,
|
|
42
|
+
orgId: orgScope.orgId,
|
|
43
|
+
...(orgScope.orgScope === "personal"
|
|
44
|
+
? { orgScope: "personal" }
|
|
45
|
+
: {}),
|
|
46
|
+
};
|
|
31
47
|
const meta = {
|
|
32
48
|
actionName: AGENT_ENGINE_DISCONNECT_ACTION_NAME,
|
|
33
49
|
caller: "http",
|
|
@@ -88,19 +104,36 @@ export async function selectDefaultModelForSavedKey(event, input) {
|
|
|
88
104
|
if (!userEmail)
|
|
89
105
|
return { status: "skipped", reason: "not-allowed" };
|
|
90
106
|
let orgId;
|
|
107
|
+
let orgScope;
|
|
91
108
|
if (input.keyScope === "org") {
|
|
92
109
|
orgId = input.keyScopeId;
|
|
93
110
|
}
|
|
94
111
|
else {
|
|
95
|
-
|
|
112
|
+
const resolved = await resolveAgentRunOrgScope({
|
|
113
|
+
event,
|
|
114
|
+
ownerContext: { owner: userEmail, anonymous: false },
|
|
115
|
+
});
|
|
116
|
+
orgId = resolved.orgId;
|
|
117
|
+
orgScope = resolved.orgScope;
|
|
118
|
+
if (!orgId && orgScope !== "personal") {
|
|
119
|
+
return { status: "skipped", reason: "not-allowed" };
|
|
120
|
+
}
|
|
96
121
|
if (orgId)
|
|
97
122
|
return { status: "skipped", reason: "personal-key" };
|
|
98
123
|
}
|
|
99
|
-
const ctx = {
|
|
124
|
+
const ctx = {
|
|
125
|
+
userEmail,
|
|
126
|
+
orgId: orgId ?? null,
|
|
127
|
+
...(orgScope ? { orgScope } : {}),
|
|
128
|
+
};
|
|
100
129
|
// Loaded here so the key-save route doesn't pull the engine registry into
|
|
101
130
|
// its module graph for saves that don't touch the default.
|
|
102
131
|
const { selectDefaultAgentEngine } = await import("../scripts/agent-engines/set-agent-engine.js");
|
|
103
|
-
const result = await runWithRequestContext({
|
|
132
|
+
const result = await runWithRequestContext({
|
|
133
|
+
userEmail,
|
|
134
|
+
...(orgId ? { orgId } : {}),
|
|
135
|
+
...(orgScope ? { orgScope } : {}),
|
|
136
|
+
}, () => selectDefaultAgentEngine(input.request, { actionName: AGENT_ENGINE_API_KEY_ACTION_NAME, caller: "http" }, ctx));
|
|
104
137
|
if (result.status === "selected") {
|
|
105
138
|
return { status: "selected", engine: result.engine, model: result.model };
|
|
106
139
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export declare function createAgentEngineOllamaModelsHandler(): import("h3").EventHandlerWithFetch<import("h3").EventHandlerRequest, Promise<{
|
|
2
2
|
error: string;
|
|
3
|
-
models?: undefined;
|
|
4
3
|
ok?: undefined;
|
|
4
|
+
models?: undefined;
|
|
5
5
|
} | {
|
|
6
|
+
error?: undefined;
|
|
6
7
|
ok: boolean;
|
|
7
8
|
models: string[];
|
|
8
|
-
error?: undefined;
|
|
9
9
|
}>>;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export declare function memoizeAgentEngineStatus<T>(identity: {
|
|
2
2
|
userEmail?: string | null;
|
|
3
3
|
orgId?: string | null;
|
|
4
|
+
orgScope?: "personal" | null;
|
|
5
|
+
isDirectLoopbackRequest?: boolean;
|
|
4
6
|
}, load: () => Promise<T>): Promise<T>;
|
|
5
7
|
export declare function getMemoizedAgentEngineStatus<T>(identity: {
|
|
6
8
|
userEmail?: string | null;
|
|
7
9
|
orgId?: string | null;
|
|
10
|
+
orgScope?: "personal" | null;
|
|
11
|
+
isDirectLoopbackRequest?: boolean;
|
|
8
12
|
}): Promise<T> | undefined;
|
|
9
13
|
export declare function invalidateAgentEngineStatusCache(): void;
|
|
10
14
|
/** @internal Test assertion for bounded status-cache retention. */
|
|
@@ -20,6 +20,8 @@ function statusCacheKey(identity) {
|
|
|
20
20
|
return JSON.stringify([
|
|
21
21
|
identity.userEmail?.trim().toLowerCase() ?? null,
|
|
22
22
|
identity.orgId?.trim() || null,
|
|
23
|
+
identity.orgScope === "personal",
|
|
24
|
+
identity.isDirectLoopbackRequest === true,
|
|
23
25
|
]);
|
|
24
26
|
}
|
|
25
27
|
export function memoizeAgentEngineStatus(identity, load) {
|
|
@@ -22,11 +22,20 @@ export declare function seedBackgroundAgentRunOwnerContext(event: H3Event, runId
|
|
|
22
22
|
export declare function resolveAgentRunOwnerContext(event: H3Event, options?: {
|
|
23
23
|
anonymousOwner?: AnonymousOwnerResolver;
|
|
24
24
|
}): Promise<AgentRunOwnerContext>;
|
|
25
|
+
export type AgentRunOrgScope = {
|
|
26
|
+
orgId?: string;
|
|
27
|
+
orgScope?: "personal";
|
|
28
|
+
};
|
|
25
29
|
export declare function resolveAgentRunOrgId(options: {
|
|
26
30
|
event: H3Event;
|
|
27
31
|
ownerContext: AgentRunOwnerContext;
|
|
28
32
|
resolveOrgId?: OrgIdResolver;
|
|
29
33
|
}): Promise<string | undefined>;
|
|
34
|
+
export declare function resolveAgentRunOrgScope(options: {
|
|
35
|
+
event: H3Event;
|
|
36
|
+
ownerContext: AgentRunOwnerContext;
|
|
37
|
+
resolveOrgId?: OrgIdResolver;
|
|
38
|
+
}): Promise<AgentRunOrgScope>;
|
|
30
39
|
export declare function resolveAgentRunRequestContext(options: {
|
|
31
40
|
event: H3Event;
|
|
32
41
|
ownerContext: AgentRunOwnerContext;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { createError, getHeader } from "h3";
|
|
2
|
-
import { AGENT_CHAT_BROWSER_SESSION_ID_FIELD } from "../agent/durable-background.js";
|
|
2
|
+
import { AGENT_CHAT_BROWSER_SESSION_ID_FIELD, AGENT_CHAT_REQUEST_CONTEXT_FIELD, } from "../agent/durable-background.js";
|
|
3
3
|
import { ANALYTICS_CLIENT_PLATFORM_BODY_FIELD, ANALYTICS_CLIENT_PLATFORM_HEADER, normalizeAnalyticsClientPlatform, } from "../shared/analytics-platform.js";
|
|
4
4
|
import { SYNTHETIC_TRAFFIC_HEADER, isSyntheticTrafficValue, } from "../shared/test-traffic.js";
|
|
5
5
|
import { CREDENTIAL_MEMBERSHIP_UNAVAILABLE_MESSAGE, isCredentialMembershipUnavailable, } from "./credential-membership-unavailable.js";
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import { isDirectLoopbackRequest, isLoopbackRequest } from "./loopback.js";
|
|
7
|
+
import { getRequestContext, getRequestIdentityAuthenticatedAtMs, getRequestIdentitySessionToken, hasExplicitPersonalOrgScope, runWithRequestContext, } from "./request-context.js";
|
|
8
|
+
import { getForwardedRequestOrigin } from "./request-origin.js";
|
|
9
|
+
const resolveOrgScopeForEmail = (...args) => import("../org/context.js").then(({ resolveOrgScopeForEmail }) => resolveOrgScopeForEmail(...args));
|
|
10
|
+
async function hasOrgScopeResolutionUnavailable(event) {
|
|
11
|
+
const { hasOrgScopeResolutionUnavailable } = await import("../org/context.js");
|
|
12
|
+
return hasOrgScopeResolutionUnavailable(event);
|
|
13
|
+
}
|
|
8
14
|
const getOrgContext = (...args) => import("../org/context.js").then(({ getOrgContext }) => getOrgContext(...args));
|
|
9
15
|
export const AGENT_RUN_OWNER_CONTEXT_KEY = "__agentNativeOwnerContext";
|
|
10
16
|
function eventContext(event) {
|
|
@@ -153,10 +159,23 @@ export async function resolveAgentRunOwnerContext(event, options = {}) {
|
|
|
153
159
|
});
|
|
154
160
|
}
|
|
155
161
|
export async function resolveAgentRunOrgId(options) {
|
|
156
|
-
|
|
157
|
-
|
|
162
|
+
return (await resolveAgentRunOrgScope(options)).orgId;
|
|
163
|
+
}
|
|
164
|
+
export async function resolveAgentRunOrgScope(options) {
|
|
165
|
+
const { ownerContext } = options;
|
|
166
|
+
const hasOwnerOrgId = Object.prototype.hasOwnProperty.call(ownerContext, "orgId");
|
|
167
|
+
if (hasOwnerOrgId) {
|
|
168
|
+
const orgId = normalizeId(ownerContext.orgId);
|
|
169
|
+
if (orgId)
|
|
170
|
+
return { orgId };
|
|
171
|
+
return ownerContext.orgScope === "personal" ? { orgScope: "personal" } : {};
|
|
172
|
+
}
|
|
173
|
+
if (ownerContext.orgScope === "personal" ||
|
|
174
|
+
hasExplicitPersonalOrgScope(options.event)) {
|
|
175
|
+
return { orgScope: "personal" };
|
|
158
176
|
}
|
|
159
177
|
let resolvedOrgId;
|
|
178
|
+
let orgLookupFailed = false;
|
|
160
179
|
if (options.resolveOrgId) {
|
|
161
180
|
resolvedOrgId = normalizeId(await options.resolveOrgId(options.event));
|
|
162
181
|
}
|
|
@@ -167,32 +186,57 @@ export async function resolveAgentRunOrgId(options) {
|
|
|
167
186
|
resolvedOrgId = normalizeId(session?.orgId);
|
|
168
187
|
}
|
|
169
188
|
catch {
|
|
170
|
-
|
|
189
|
+
orgLookupFailed = true;
|
|
171
190
|
}
|
|
172
191
|
if (!resolvedOrgId) {
|
|
173
192
|
try {
|
|
174
193
|
const orgContext = await getOrgContext(options.event);
|
|
175
194
|
resolvedOrgId = normalizeId(orgContext.orgId);
|
|
195
|
+
const scopeUnavailable = await hasOrgScopeResolutionUnavailable(options.event);
|
|
196
|
+
if (!resolvedOrgId && scopeUnavailable)
|
|
197
|
+
orgLookupFailed = true;
|
|
198
|
+
if (!resolvedOrgId &&
|
|
199
|
+
!scopeUnavailable &&
|
|
200
|
+
!orgLookupFailed &&
|
|
201
|
+
ownerContext.owner &&
|
|
202
|
+
!ownerContext.anonymous &&
|
|
203
|
+
normalizeId(orgContext.email)?.toLowerCase() ===
|
|
204
|
+
ownerContext.owner.trim().toLowerCase()) {
|
|
205
|
+
return { orgScope: "personal" };
|
|
206
|
+
}
|
|
176
207
|
}
|
|
177
208
|
catch {
|
|
178
|
-
|
|
209
|
+
orgLookupFailed = true;
|
|
179
210
|
}
|
|
180
211
|
}
|
|
181
212
|
}
|
|
182
|
-
if (!resolvedOrgId &&
|
|
183
|
-
options.ownerContext.owner &&
|
|
184
|
-
!options.ownerContext.anonymous) {
|
|
213
|
+
if (!resolvedOrgId && ownerContext.owner && !ownerContext.anonymous) {
|
|
185
214
|
try {
|
|
186
|
-
|
|
215
|
+
const scope = await resolveOrgScopeForEmail(ownerContext.owner);
|
|
216
|
+
if (scope.status === "organization") {
|
|
217
|
+
resolvedOrgId = normalizeId(scope.orgId);
|
|
218
|
+
}
|
|
219
|
+
else if (scope.status === "unknown") {
|
|
220
|
+
orgLookupFailed = true;
|
|
221
|
+
}
|
|
222
|
+
else if (!orgLookupFailed) {
|
|
223
|
+
return { orgScope: "personal" };
|
|
224
|
+
}
|
|
187
225
|
}
|
|
188
226
|
catch {
|
|
189
|
-
|
|
227
|
+
orgLookupFailed = true;
|
|
190
228
|
}
|
|
191
229
|
}
|
|
192
|
-
|
|
230
|
+
if (resolvedOrgId)
|
|
231
|
+
return { orgId: resolvedOrgId };
|
|
232
|
+
// An absent org is personal only when every available membership lookup succeeded.
|
|
233
|
+
if (!orgLookupFailed && ownerContext.owner && !ownerContext.anonymous) {
|
|
234
|
+
return { orgScope: "personal" };
|
|
235
|
+
}
|
|
236
|
+
return {};
|
|
193
237
|
}
|
|
194
238
|
export async function resolveAgentRunRequestContext(options) {
|
|
195
|
-
const orgId = await
|
|
239
|
+
const { orgId, orgScope } = await resolveAgentRunOrgScope(options);
|
|
196
240
|
const timezone = readAgentRunTimezone(options.event);
|
|
197
241
|
const browserSessionId = options.isBackgroundWorker
|
|
198
242
|
? readDurableBrowserSessionId(options.event)
|
|
@@ -202,6 +246,17 @@ export async function resolveAgentRunRequestContext(options) {
|
|
|
202
246
|
const clientPlatform = readAnalyticsClientPlatformHeader(options.event);
|
|
203
247
|
const isSyntheticTraffic = readSyntheticTrafficHeader(options.event);
|
|
204
248
|
const waitUntil = requestWaitUntil(options.event);
|
|
249
|
+
const durableBody = options.isBackgroundWorker
|
|
250
|
+
? eventContext(options.event)["__agentChatBackgroundBody"]
|
|
251
|
+
: undefined;
|
|
252
|
+
const durableRequestContext = durableBody?.[AGENT_CHAT_REQUEST_CONTEXT_FIELD];
|
|
253
|
+
const durableOrigin = durableRequestContext &&
|
|
254
|
+
typeof durableRequestContext === "object" &&
|
|
255
|
+
!Array.isArray(durableRequestContext) &&
|
|
256
|
+
typeof durableRequestContext.requestOrigin ===
|
|
257
|
+
"string"
|
|
258
|
+
? durableRequestContext.requestOrigin
|
|
259
|
+
: undefined;
|
|
205
260
|
const run = {
|
|
206
261
|
...(options.isBackgroundWorker ? { isBackgroundWorker: true } : {}),
|
|
207
262
|
...(waitUntil ? { waitUntil } : {}),
|
|
@@ -211,10 +266,11 @@ export async function resolveAgentRunRequestContext(options) {
|
|
|
211
266
|
...(options.ownerContext.authUserId
|
|
212
267
|
? { authUserId: options.ownerContext.authUserId }
|
|
213
268
|
: {}),
|
|
214
|
-
...(
|
|
215
|
-
? { orgScope: "personal" }
|
|
216
|
-
: {}),
|
|
269
|
+
...(orgScope === "personal" ? { orgScope: "personal" } : {}),
|
|
217
270
|
...(options.ownerContext.anonymous ? { agentRunAnonymous: true } : {}),
|
|
271
|
+
...(options.ownerContext.anonymous && !orgId
|
|
272
|
+
? { defaultAgentEngineScope: "deployment" }
|
|
273
|
+
: {}),
|
|
218
274
|
...(options.ownerContext.identityAuthenticatedAtMs !== undefined
|
|
219
275
|
? {
|
|
220
276
|
identityAuthenticatedAtMs: options.ownerContext.identityAuthenticatedAtMs,
|
|
@@ -227,6 +283,15 @@ export async function resolveAgentRunRequestContext(options) {
|
|
|
227
283
|
...(browserSessionId ? { browserSessionId } : {}),
|
|
228
284
|
...(clientPlatform ? { clientPlatform } : {}),
|
|
229
285
|
...(isSyntheticTraffic ? { isSyntheticTraffic: true } : {}),
|
|
286
|
+
requestOrigin: options.isBackgroundWorker
|
|
287
|
+
? durableOrigin
|
|
288
|
+
: getForwardedRequestOrigin(options.event),
|
|
289
|
+
isLoopbackRequest: options.isBackgroundWorker
|
|
290
|
+
? false
|
|
291
|
+
: isLoopbackRequest(options.event),
|
|
292
|
+
isDirectLoopbackRequest: options.isBackgroundWorker
|
|
293
|
+
? false
|
|
294
|
+
: isDirectLoopbackRequest(options.event),
|
|
230
295
|
...(browserTabId ? { run: { ...run, browserTabId } } : {}),
|
|
231
296
|
...(!browserTabId && Object.keys(run).length > 0 ? { run } : {}),
|
|
232
297
|
};
|
|
@@ -11,7 +11,7 @@ import { readDeployCredentialEnv } from "./credential-provider.js";
|
|
|
11
11
|
import { getWorkspaceA2ADerivedSecret } from "./derived-secret.js";
|
|
12
12
|
import { publicFrameworkPath } from "./framework-route-prefix.js";
|
|
13
13
|
import { getAppBasePath, getOrigin, getOAuthStateSigningKey, isAllowedOAuthRedirectUri, isConfiguredAppOrigin, } from "./google-oauth.js";
|
|
14
|
-
import { isLoopbackOrigin } from "./origin-allowlist.js";
|
|
14
|
+
import { getAppDeclaredOrigins, isLoopbackOrigin } from "./origin-allowlist.js";
|
|
15
15
|
import { getRequestOrgId, getRequestUserEmail } from "./request-context.js";
|
|
16
16
|
const DEFAULT_BUILDER_APP_HOST = "https://builder.io";
|
|
17
17
|
const DEFAULT_BUILDER_API_HOST = "https://api.builder.io";
|
|
@@ -981,7 +981,7 @@ function getBuilderConnectCallbackOrigin(event, originOverride) {
|
|
|
981
981
|
if (isBuilderCloudRequestHost(headerHost)) {
|
|
982
982
|
return getBuilderBrowserOriginForEvent(event);
|
|
983
983
|
}
|
|
984
|
-
const configuredOrigin =
|
|
984
|
+
const configuredOrigin = getBuilderAppOrigin(event);
|
|
985
985
|
if (!isLoopbackOrigin(configuredOrigin))
|
|
986
986
|
return configuredOrigin;
|
|
987
987
|
if (!isLoopbackBuilderRequestHost(headerHost) &&
|
|
@@ -992,6 +992,21 @@ function getBuilderConnectCallbackOrigin(event, originOverride) {
|
|
|
992
992
|
}
|
|
993
993
|
return configuredOrigin;
|
|
994
994
|
}
|
|
995
|
+
/**
|
|
996
|
+
* A proxy that rewrites `Host` (Builder Cloud -> `*.netlify.app`) hands us an
|
|
997
|
+
* origin the browser never visited, and the origin lock 404s it. When the
|
|
998
|
+
* inbound origin is not one the app declared, send the browser to the first
|
|
999
|
+
* declared origin instead.
|
|
1000
|
+
*/
|
|
1001
|
+
function getBuilderAppOrigin(event) {
|
|
1002
|
+
const inbound = getOrigin(event, { useForwardedHost: false });
|
|
1003
|
+
if (process.env.NODE_ENV !== "production")
|
|
1004
|
+
return inbound;
|
|
1005
|
+
const declared = getAppDeclaredOrigins().filter((origin) => !isLoopbackOrigin(origin));
|
|
1006
|
+
if (declared.length === 0 || declared.includes(inbound))
|
|
1007
|
+
return inbound;
|
|
1008
|
+
return declared[0];
|
|
1009
|
+
}
|
|
995
1010
|
function isAllowedBuilderConnectOrigin(origin) {
|
|
996
1011
|
let url;
|
|
997
1012
|
try {
|
|
@@ -1126,7 +1141,7 @@ export function getBuilderBrowserOriginForEvent(event) {
|
|
|
1126
1141
|
return getConfiguredBuilderFallbackOrigin(event) ?? "";
|
|
1127
1142
|
}
|
|
1128
1143
|
if (!isTrustedBuilderRequestHost(headerHost)) {
|
|
1129
|
-
return
|
|
1144
|
+
return getBuilderAppOrigin(event);
|
|
1130
1145
|
}
|
|
1131
1146
|
if (isLoopbackBuilderRequestHost(headerHost)) {
|
|
1132
1147
|
const publicPreviewOrigin = firstPublicBuilderPreviewOriginFromEnv();
|
|
@@ -10,9 +10,10 @@ import { listOAuthAccountsByOwner, markOAuthReconnectRequired, readOAuthCredenti
|
|
|
10
10
|
import { encryptSecretValue, decryptSecretValue } from "../secrets/crypto.js";
|
|
11
11
|
import { getSetting, mutateSetting } from "../settings/store.js";
|
|
12
12
|
import { getUserSetting, mutateUserSetting, putUserSetting, deleteUserSetting, } from "../settings/user-settings.js";
|
|
13
|
-
import { getSession,
|
|
13
|
+
import { getSession, redirectWithStagedCookies } from "./auth.js";
|
|
14
14
|
import { oauthErrorPage } from "./google-oauth.js";
|
|
15
|
-
import {
|
|
15
|
+
import { isDirectLoopbackRequest } from "./loopback.js";
|
|
16
|
+
import { getRequestContext, getRequestRunContext } from "./request-context.js";
|
|
16
17
|
export const CHATGPT_SUBSCRIPTION_OAUTH_PROVIDER = CHATGPT_SUBSCRIPTION_PROVIDER;
|
|
17
18
|
const CHATGPT_OAUTH_FLOW_COOKIE = "an_chatgpt_subscription_oauth";
|
|
18
19
|
const CHATGPT_OAUTH_FLOW_TTL_SECONDS = 10 * 60;
|
|
@@ -146,23 +147,10 @@ export function isChatGPTSubscriptionLoopbackCallbackUri(value) {
|
|
|
146
147
|
url.port !== "0" &&
|
|
147
148
|
url.href === value);
|
|
148
149
|
}
|
|
149
|
-
function exactLoopbackSocketAddress(address) {
|
|
150
|
-
return address === "127.0.0.1" || address === "::ffff:127.0.0.1";
|
|
151
|
-
}
|
|
152
150
|
function callbackUriForEvent(event) {
|
|
153
151
|
const host = getRequestHeader(event, "host");
|
|
154
|
-
|
|
155
|
-
const socket = request?.socket;
|
|
156
|
-
if (!host ||
|
|
157
|
-
host.trim() !== host ||
|
|
158
|
-
getRequestHeader(event, "x-forwarded-host") ||
|
|
159
|
-
getRequestHeader(event, "x-forwarded-proto") ||
|
|
160
|
-
!isLoopbackRequest(event) ||
|
|
161
|
-
!socket ||
|
|
162
|
-
("encrypted" in socket && socket.encrypted) ||
|
|
163
|
-
!exactLoopbackSocketAddress(socket.localAddress)) {
|
|
152
|
+
if (!host || !isDirectLoopbackRequest(event))
|
|
164
153
|
return null;
|
|
165
|
-
}
|
|
166
154
|
const originValue = `http://${host}`;
|
|
167
155
|
if (!URL.canParse(originValue))
|
|
168
156
|
return null;
|
|
@@ -181,9 +169,16 @@ function callbackUriForEvent(event) {
|
|
|
181
169
|
: null;
|
|
182
170
|
}
|
|
183
171
|
function currentRequestIsLocalLoopback() {
|
|
172
|
+
if (getRequestRunContext()?.isBackgroundWorker === true)
|
|
173
|
+
return false;
|
|
184
174
|
const context = getRequestContext();
|
|
185
|
-
if (!context?.isLoopbackRequest ||
|
|
175
|
+
if (!context?.isLoopbackRequest ||
|
|
176
|
+
!context.isDirectLoopbackRequest ||
|
|
177
|
+
!context.requestOrigin ||
|
|
178
|
+
context.orgId ||
|
|
179
|
+
context.orgScope !== "personal") {
|
|
186
180
|
return false;
|
|
181
|
+
}
|
|
187
182
|
if (!URL.canParse(context.requestOrigin))
|
|
188
183
|
return false;
|
|
189
184
|
const origin = new URL(context.requestOrigin);
|
|
@@ -31,6 +31,7 @@ export declare const LEGACY_FRAMEWORK_EVENTS_ROUTE = "/_agent-native/poll-events
|
|
|
31
31
|
export declare function resolveAgentEngineStatusIdentity(event: H3Event): Promise<{
|
|
32
32
|
userEmail: string | undefined;
|
|
33
33
|
orgId: string | undefined;
|
|
34
|
+
orgScope?: "personal";
|
|
34
35
|
}>;
|
|
35
36
|
/**
|
|
36
37
|
* Shared Builder grants stay org-scoped, but connect initiation can come from
|
|
@@ -28,7 +28,7 @@ import { handleMcpOAuth, handleMcpOAuthAuthorizationServerMetadata, handleMcpOAu
|
|
|
28
28
|
import { MCP_ROUTE_PREFIXES } from "../mcp/route-paths.js";
|
|
29
29
|
import { isSlackWebhookConfigured, registerBuiltinNotificationChannels, } from "../notifications/channels.js";
|
|
30
30
|
import { createNotificationsHandler } from "../notifications/routes.js";
|
|
31
|
-
import { getOrgContext } from "../org/context.js";
|
|
31
|
+
import { getOrgContext, hasOrgScopeResolutionUnavailable, resolveOrgScopeForEmail, } from "../org/context.js";
|
|
32
32
|
import { isMissingOrganizationTableError } from "../org/membership.js";
|
|
33
33
|
import { createProgressHandler } from "../progress/routes.js";
|
|
34
34
|
import { REALTIME_POLL_LIVE_QUERY_PARAM } from "../realtime-protocol.js";
|
|
@@ -84,6 +84,7 @@ import { getAppBasePath, getOrigin } from "./google-oauth.js";
|
|
|
84
84
|
import { createGoogleRealtimeSessionHandler } from "./google-realtime-session.js";
|
|
85
85
|
import { readBody, readBodyWithSizeLimit, DEFAULT_UPLOAD_MAX_FILE_BYTES, isAllowedUploadMimeType, } from "./h3-helpers.js";
|
|
86
86
|
import { handleIdentitySso } from "./identity-sso.js";
|
|
87
|
+
import { isDirectLoopbackRequest, isLoopbackRequest } from "./loopback.js";
|
|
87
88
|
import { createOpenRouteHandler } from "./open-route.js";
|
|
88
89
|
import { PERSONAL_PROVIDER_KEYS_RESTRICTED_ERROR_CODE, PERSONAL_PROVIDER_KEYS_RESTRICTED_MESSAGE, readOrgMemberRole, } from "./personal-provider-key-policy.js";
|
|
89
90
|
import { createPollEventsHandler, validateSseMaxDurationMs, } from "./poll-events.js";
|
|
@@ -146,7 +147,13 @@ function requestAgentEngineStatusDeps() {
|
|
|
146
147
|
const appId = normalizeAgentAppModelDefaultAppId(app.id ?? app.template ?? app.slug);
|
|
147
148
|
if (!appId)
|
|
148
149
|
return null;
|
|
149
|
-
const settings = await readAgentAppModelDefaultSettings({
|
|
150
|
+
const settings = await readAgentAppModelDefaultSettings({
|
|
151
|
+
userEmail: getRequestUserEmail(),
|
|
152
|
+
orgId: getRequestOrgId(),
|
|
153
|
+
...(getRequestContext()?.orgScope === "personal"
|
|
154
|
+
? { orgScope: "personal" }
|
|
155
|
+
: {}),
|
|
156
|
+
}, appId);
|
|
150
157
|
return settings.engine && settings.model
|
|
151
158
|
? { engine: settings.engine, model: settings.model }
|
|
152
159
|
: null;
|
|
@@ -177,7 +184,21 @@ export async function resolveAgentEngineStatusIdentity(event) {
|
|
|
177
184
|
return { userEmail: undefined, orgId: undefined };
|
|
178
185
|
try {
|
|
179
186
|
const orgCtx = await getOrgContext(event);
|
|
180
|
-
|
|
187
|
+
if (orgCtx.orgId)
|
|
188
|
+
return { userEmail, orgId: orgCtx.orgId };
|
|
189
|
+
if (hasOrgScopeResolutionUnavailable(event)) {
|
|
190
|
+
return { userEmail, orgId: undefined };
|
|
191
|
+
}
|
|
192
|
+
if (orgCtx.email.trim().toLowerCase() === userEmail.trim().toLowerCase()) {
|
|
193
|
+
return { userEmail, orgId: undefined, orgScope: "personal" };
|
|
194
|
+
}
|
|
195
|
+
const scope = await resolveOrgScopeForEmail(userEmail);
|
|
196
|
+
if (scope.status === "organization") {
|
|
197
|
+
return { userEmail, orgId: scope.orgId };
|
|
198
|
+
}
|
|
199
|
+
return scope.status === "personal"
|
|
200
|
+
? { userEmail, orgId: undefined, orgScope: "personal" }
|
|
201
|
+
: { userEmail, orgId: undefined };
|
|
181
202
|
}
|
|
182
203
|
catch (error) {
|
|
183
204
|
/* org module not present in this template */
|
|
@@ -1007,10 +1028,10 @@ export function resolveAvatarEmailParam(pathname, appBasePath = "") {
|
|
|
1007
1028
|
}
|
|
1008
1029
|
async function detectUsageEngineName(event, userEmail) {
|
|
1009
1030
|
try {
|
|
1010
|
-
const
|
|
1011
|
-
?
|
|
1012
|
-
: undefined;
|
|
1013
|
-
const status = await runWithRequestContext(
|
|
1031
|
+
const identity = userEmail
|
|
1032
|
+
? await resolveAgentEngineStatusIdentity(event)
|
|
1033
|
+
: { userEmail, orgId: undefined };
|
|
1034
|
+
const status = await runWithRequestContext(identity, () => resolveAgentEngineStatus({
|
|
1014
1035
|
...requestAgentEngineStatusDeps(),
|
|
1015
1036
|
// Tracking only needs the engine name; skip the base-URL secret read.
|
|
1016
1037
|
readOpenAiBaseUrlConfigured: () => false,
|
|
@@ -3990,8 +4011,24 @@ export function createCoreRoutesPlugin(options = {}) {
|
|
|
3990
4011
|
getH3App(nitroApp).use(`${P}/agent-engine/status`, defineEventHandler(async (event) => {
|
|
3991
4012
|
try {
|
|
3992
4013
|
const statusStartedAt = Date.now();
|
|
3993
|
-
const { userEmail, orgId } = await resolveAgentEngineStatusIdentity(event);
|
|
3994
|
-
const
|
|
4014
|
+
const { userEmail, orgId, orgScope } = await resolveAgentEngineStatusIdentity(event);
|
|
4015
|
+
const directLoopbackRequest = isDirectLoopbackRequest(event);
|
|
4016
|
+
const requestOrigin = directLoopbackRequest
|
|
4017
|
+
? `http://${getHeader(event, "host")}`
|
|
4018
|
+
: undefined;
|
|
4019
|
+
const status = await memoizeAgentEngineStatus({
|
|
4020
|
+
userEmail,
|
|
4021
|
+
orgId,
|
|
4022
|
+
orgScope,
|
|
4023
|
+
isDirectLoopbackRequest: directLoopbackRequest,
|
|
4024
|
+
}, async () => await runWithRequestContext({
|
|
4025
|
+
userEmail,
|
|
4026
|
+
orgId,
|
|
4027
|
+
...(orgScope ? { orgScope } : {}),
|
|
4028
|
+
isLoopbackRequest: isLoopbackRequest(event),
|
|
4029
|
+
isDirectLoopbackRequest: directLoopbackRequest,
|
|
4030
|
+
...(requestOrigin ? { requestOrigin } : {}),
|
|
4031
|
+
}, async () => {
|
|
3995
4032
|
const deps = requestAgentEngineStatusDeps();
|
|
3996
4033
|
const engineStatus = await resolveAgentEngineStatus(deps);
|
|
3997
4034
|
const chatEligible = await isAgentChatAiSetupReady({
|