@lobu/runtime-device 20.2.0 → 21.1.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/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +44 -38
- package/npm-shrinkwrap.json +277 -231
- package/package.json +1 -1
- package/vendor/cli/dist/commands/_lib/apply/apply-cmd.d.ts +7 -2
- package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +77 -79
- package/vendor/cli/dist/commands/_lib/apply/client.d.ts +41 -29
- package/vendor/cli/dist/commands/_lib/apply/client.js +33 -47
- package/vendor/cli/dist/commands/_lib/apply/deployment.d.ts +1 -1
- package/vendor/cli/dist/commands/_lib/apply/deployment.js +2 -3
- package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +47 -13
- package/vendor/cli/dist/commands/_lib/apply/desired-state.js +106 -7
- package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +14 -7
- package/vendor/cli/dist/commands/_lib/apply/diff.js +42 -29
- package/vendor/cli/dist/commands/_lib/apply/map-config.js +2 -3
- package/vendor/cli/dist/commands/_lib/apply/render.js +2 -0
- package/vendor/cli/dist/commands/_lib/apply/rollback-cmd.js +3 -3
- package/vendor/cli/dist/commands/_lib/connector-loader.d.ts +5 -1
- package/vendor/cli/dist/commands/_lib/connector-loader.js +1 -1
- package/vendor/cli/dist/commands/_lib/ensure-deps-installed.d.ts +15 -26
- package/vendor/cli/dist/commands/_lib/ensure-deps-installed.js +227 -136
- package/vendor/cli/dist/commands/_lib/view-bundler.d.ts +33 -0
- package/vendor/cli/dist/commands/_lib/view-bundler.js +211 -0
- package/vendor/cli/dist/commands/_lib/view-reapply.d.ts +60 -0
- package/vendor/cli/dist/commands/_lib/view-reapply.js +182 -0
- package/vendor/cli/dist/commands/dev.d.ts +14 -5
- package/vendor/cli/dist/commands/dev.js +56 -8
- package/vendor/cli/dist/commands/init.d.ts +1 -1
- package/vendor/cli/dist/commands/init.js +4 -4
- package/vendor/cli/dist/commands/memory/_lib/schema.d.ts +0 -1
- package/vendor/cli/dist/commands/memory/_lib/schema.js +0 -9
- package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +0 -2
- package/vendor/cli/dist/config/define.d.ts +25 -8
- package/vendor/cli/dist/config/define.js +24 -0
- package/vendor/cli/dist/connectors/README.md +15 -16
- package/vendor/cli/dist/connectors/github.ts +274 -193
- package/vendor/cli/dist/connectors/google_calendar.ts +54 -160
- package/vendor/cli/dist/connectors/google_drive.ts +106 -131
- package/vendor/cli/dist/connectors/google_gmail.ts +53 -30
- package/vendor/cli/dist/connectors/hackernews.ts +2 -5
- package/vendor/cli/dist/connectors/jira.ts +71 -27
- package/vendor/cli/dist/connectors/linear.ts +55 -21
- package/vendor/cli/dist/connectors/microsoft_outlook.ts +151 -110
- package/vendor/cli/dist/connectors/postgres.ts +19 -11
- package/vendor/cli/dist/connectors/producthunt.ts +11 -7
- package/vendor/cli/dist/connectors/reddit.ts +36 -6
- package/vendor/cli/dist/connectors/rss.ts +2 -2
- package/vendor/cli/dist/connectors/whatsapp_web.ts +5 -4
- package/vendor/cli/dist/connectors/x.ts +32 -17
- package/vendor/cli/dist/connectors/youtube.ts +18 -5
- package/vendor/cli/dist/internal/connector-compiler.d.ts +1 -1
- package/vendor/cli/dist/internal/connector-compiler.js +216 -17
- package/vendor/cli/dist/providers.json +0 -1
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/package.json +2 -1
- package/vendor/connector-sdk/dist/connector-runtime.d.ts +4 -6
- package/vendor/connector-sdk/dist/connector-runtime.js +3 -5
- package/vendor/connector-sdk/dist/connector-types.d.ts +28 -15
- package/vendor/connector-sdk/dist/define-connector.d.ts +4 -1
- package/vendor/connector-sdk/dist/define-connector.js +4 -1
- package/vendor/connector-sdk/dist/http-client.d.ts +18 -7
- package/vendor/connector-sdk/dist/http-client.js +15 -13
- package/vendor/connector-sdk/dist/index.d.ts +1 -1
- package/vendor/connector-sdk/dist/retry.d.ts +15 -3
- package/vendor/connector-sdk/dist/retry.js +40 -64
- package/vendor/connector-sdk/package.json +1 -1
- package/vendor/connector-worker/dist/agent-turn/guest-entry.js +122 -10
- package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +78 -10
- package/vendor/connector-worker/dist/agent-turn/types.d.ts +34 -4
- package/vendor/connector-worker/dist/compile/index.d.ts +21 -2
- package/vendor/connector-worker/dist/compile/index.js +41 -8
- package/vendor/connector-worker/dist/compile/source-capture.d.ts +10 -0
- package/vendor/connector-worker/dist/compile/source-capture.js +108 -0
- package/vendor/connector-worker/dist/daemon/agent-turn.js +97 -10
- package/vendor/connector-worker/dist/daemon/automation.d.ts +1 -1
- package/vendor/connector-worker/dist/daemon/client.d.ts +4 -1
- package/vendor/connector-worker/dist/daemon/client.js +3 -0
- package/vendor/connector-worker/dist/daemon/executor.d.ts +0 -1
- package/vendor/connector-worker/dist/daemon/executor.js +46 -51
- package/vendor/connector-worker/dist/daemon/start.js +6 -2
- package/vendor/connector-worker/dist/executor/interface.d.ts +19 -9
- package/vendor/connector-worker/dist/executor/isolate.js +85 -28
- package/vendor/connector-worker/dist/self-check/index.js +15 -13
- package/vendor/connector-worker/package.json +1 -1
- package/vendor/core/dist/agent-policy.js +3 -0
- package/vendor/core/dist/connector-query-errors.d.ts +3 -3
- package/vendor/core/dist/connector-query-errors.js +8 -3
- package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +14 -0
- package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +12 -1
- package/vendor/core/dist/contracts/tools/manage-automations.d.ts +0 -10
- package/vendor/core/dist/contracts/tools/manage-automations.js +0 -33
- package/vendor/core/dist/contracts/tools/manage-classifiers.d.ts +4 -34
- package/vendor/core/dist/contracts/tools/manage-classifiers.js +13 -44
- package/vendor/core/dist/contracts/tools/manage-connections.d.ts +24 -0
- package/vendor/core/dist/contracts/tools/manage-connections.js +13 -0
- package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +15 -25
- package/vendor/core/dist/contracts/tools/manage-entity-schema.js +3 -5
- package/vendor/core/dist/contracts/tools/manage-entity.d.ts +0 -9
- package/vendor/core/dist/contracts/tools/manage-entity.js +0 -6
- package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +11 -0
- package/vendor/core/dist/contracts/tools/manage-feeds.js +12 -1
- package/vendor/core/dist/contracts/tools/manage-operations.d.ts +47 -6
- package/vendor/core/dist/contracts/tools/manage-operations.js +36 -1
- package/vendor/core/dist/contracts/tools/manage-views.d.ts +293 -0
- package/vendor/core/dist/contracts/tools/manage-views.js +203 -0
- package/vendor/core/dist/contracts/tools/source-files-schema.d.ts +7 -0
- package/vendor/core/dist/contracts/tools/source-files-schema.js +19 -0
- package/vendor/core/dist/contracts/tools/source-files.d.ts +14 -0
- package/vendor/core/dist/contracts/tools/source-files.js +55 -0
- package/vendor/core/dist/contracts/tools/view-attach.d.ts +20 -0
- package/vendor/core/dist/contracts/tools/view-attach.js +45 -0
- package/vendor/core/dist/contracts/tools/view-content-hash.d.ts +37 -0
- package/vendor/core/dist/contracts/tools/view-content-hash.js +58 -0
- package/vendor/core/dist/contracts/tools/view-path.d.ts +18 -0
- package/vendor/core/dist/contracts/tools/view-path.js +24 -0
- package/vendor/core/dist/contracts/worker/protocol.d.ts +90 -8
- package/vendor/core/dist/contracts/worker/protocol.js +70 -10
- package/vendor/core/dist/index.d.ts +1 -1
- package/vendor/core/dist/provider-config-types.d.ts +0 -9
- package/vendor/core/dist/reserved.d.ts +3 -3
- package/vendor/core/dist/reserved.js +9 -1
- package/vendor/core/dist/sdk-compat.d.ts +1 -1
- package/vendor/core/dist/utils/secret-redaction.js +2 -0
- package/vendor/core/package.json +30 -5
- package/vendor/core/dist/contracts/tools/manage-view-templates.d.ts +0 -165
- package/vendor/core/dist/contracts/tools/manage-view-templates.js +0 -130
|
@@ -36,13 +36,11 @@ export class ConnectorRuntime {
|
|
|
36
36
|
/**
|
|
37
37
|
* Sync data from the connected service.
|
|
38
38
|
*
|
|
39
|
-
* Called by the worker when a sync run is executed.
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* `ctx.updateCheckpoint()` to stream progress before returning.
|
|
39
|
+
* Called by the worker when a sync run is executed. Commit each page of
|
|
40
|
+
* events with its checkpoint through `ctx.commit` as it is fetched, then
|
|
41
|
+
* return how the pass ended.
|
|
43
42
|
*
|
|
44
43
|
* @param ctx - Sync context with feed config, checkpoint, and credentials
|
|
45
|
-
* @returns Events and updated checkpoint
|
|
46
44
|
*/
|
|
47
45
|
async sync(ctx) {
|
|
48
46
|
const handler = this.definition.feeds?.[ctx.feedKey]?.sync;
|
|
@@ -803,10 +803,22 @@ export interface SyncContext<C = Record<string, unknown>, F = Record<string, unk
|
|
|
803
803
|
sessionState?: Record<string, unknown> | null;
|
|
804
804
|
/** App-installation context when this connection is backed by an App install. */
|
|
805
805
|
installation?: ConnectorInstallationContext;
|
|
806
|
-
/**
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
806
|
+
/**
|
|
807
|
+
* Durably store `events`, then move the feed's checkpoint to `checkpoint`.
|
|
808
|
+
* The returned promise resolves only after both have committed and rejects
|
|
809
|
+
* (failing the run) on either failure. A large page may be stored in chunks,
|
|
810
|
+
* but its checkpoint moves only with the final chunk, so a failed commit may
|
|
811
|
+
* leave deduplicable events behind but never skips an unstored event. Commit
|
|
812
|
+
* as you go, once per source page, awaiting each call before the next: a run
|
|
813
|
+
* that stops resumes from the last checkpoint that resolved.
|
|
814
|
+
*
|
|
815
|
+
* Pass `null` to store events without moving the checkpoint, and an empty
|
|
816
|
+
* `events` array to record progress over a page that yielded nothing (a
|
|
817
|
+
* continuation token past filtered-out items). The checkpoint is opaque to
|
|
818
|
+
* the platform; the next run receives the last committed one as
|
|
819
|
+
* `ctx.checkpoint`.
|
|
820
|
+
*/
|
|
821
|
+
commit: (events: EventEnvelope[], checkpoint: C | null) => Promise<void>;
|
|
810
822
|
}
|
|
811
823
|
/**
|
|
812
824
|
* The credential a run is handed. On the isolate lane `accessToken` is a
|
|
@@ -824,15 +836,18 @@ export interface SyncCredentials {
|
|
|
824
836
|
scope?: string | null;
|
|
825
837
|
}
|
|
826
838
|
/**
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
* Generic parameter `C` matches the connector's checkpoint shape.
|
|
839
|
+
* How a sync pass ended. Its events and checkpoints were already committed
|
|
840
|
+
* through `ctx.commit`; the result only says what should happen next.
|
|
830
841
|
*/
|
|
831
|
-
export interface SyncResult
|
|
832
|
-
/**
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
842
|
+
export interface SyncResult {
|
|
843
|
+
/**
|
|
844
|
+
* `complete` — the source is caught up; the next run follows the feed's
|
|
845
|
+
* schedule. `more` — the pass stopped early at a committed boundary (a time
|
|
846
|
+
* or size budget) with work remaining, so the platform starts the next run
|
|
847
|
+
* right away instead of waiting for the schedule. `more` is honored only
|
|
848
|
+
* when the run committed a checkpoint.
|
|
849
|
+
*/
|
|
850
|
+
status: 'complete' | 'more';
|
|
836
851
|
/** Updated auth state to persist on the linked auth profile (browser cookies, etc.) */
|
|
837
852
|
auth_update?: Record<string, unknown> | null;
|
|
838
853
|
/** Optional metadata about the sync */
|
|
@@ -925,7 +940,7 @@ export interface FeedReadResult {
|
|
|
925
940
|
/** Required for a windowed read; absence means the reader cannot honor it. */
|
|
926
941
|
window?: FeedReadWindowCoverage;
|
|
927
942
|
}
|
|
928
|
-
export type FeedSyncHandler<C = Record<string, unknown>, F = Record<string, unknown>> = (ctx: SyncContext<C, F>) => Promise<SyncResult
|
|
943
|
+
export type FeedSyncHandler<C = Record<string, unknown>, F = Record<string, unknown>> = (ctx: SyncContext<C, F>) => Promise<SyncResult>;
|
|
929
944
|
export type FeedReadHandler<F = Record<string, unknown>> = (ctx: FeedReadContext<F>) => Promise<FeedReadResult>;
|
|
930
945
|
/** Executable feed definition held only inside a connector runtime. */
|
|
931
946
|
export interface RuntimeFeedDefinition<C = Record<string, unknown>, F = Record<string, unknown>> extends Omit<FeedDefinition, 'operations'> {
|
|
@@ -1154,8 +1169,6 @@ export interface ContentItem {
|
|
|
1154
1169
|
created_at: string;
|
|
1155
1170
|
occurred_at: string;
|
|
1156
1171
|
content_date?: string;
|
|
1157
|
-
/** Excerpt for highlighted evidence (when filtering by classification value) */
|
|
1158
|
-
excerpt?: string;
|
|
1159
1172
|
/** Search score fields (only present when query is provided) */
|
|
1160
1173
|
similarity?: number;
|
|
1161
1174
|
text_rank?: number;
|
|
@@ -21,7 +21,10 @@
|
|
|
21
21
|
* ```ts
|
|
22
22
|
* export default defineConnector({
|
|
23
23
|
* key: 'github', name: 'GitHub', version: '1.0.0',
|
|
24
|
-
* feeds: { stars: { name: 'Stars', sync: async (ctx) =>
|
|
24
|
+
* feeds: { stars: { name: 'Stars', sync: async (ctx) => {
|
|
25
|
+
* await ctx.commit(events, checkpoint);
|
|
26
|
+
* return { status: 'complete' };
|
|
27
|
+
* } } },
|
|
25
28
|
* actions: { star_repo: { name: 'Star', execute: async (ctx) => ({ success: true }) } },
|
|
26
29
|
* });
|
|
27
30
|
* ```
|
|
@@ -21,7 +21,10 @@
|
|
|
21
21
|
* ```ts
|
|
22
22
|
* export default defineConnector({
|
|
23
23
|
* key: 'github', name: 'GitHub', version: '1.0.0',
|
|
24
|
-
* feeds: { stars: { name: 'Stars', sync: async (ctx) =>
|
|
24
|
+
* feeds: { stars: { name: 'Stars', sync: async (ctx) => {
|
|
25
|
+
* await ctx.commit(events, checkpoint);
|
|
26
|
+
* return { status: 'complete' };
|
|
27
|
+
* } } },
|
|
25
28
|
* actions: { star_repo: { name: 'Star', execute: async (ctx) => ({ success: true }) } },
|
|
26
29
|
* });
|
|
27
30
|
* ```
|
|
@@ -30,6 +30,16 @@ interface HttpClientRetryOptions {
|
|
|
30
30
|
context?: Record<string, any>;
|
|
31
31
|
onRetry?: (error: Error, attempt: number) => void;
|
|
32
32
|
}
|
|
33
|
+
/** `RequestInit` plus the one hint retrying needs from the caller. */
|
|
34
|
+
export interface HttpRequestInit extends RequestInit {
|
|
35
|
+
/**
|
|
36
|
+
* The request is safe to repeat although its method is not idempotent — a
|
|
37
|
+
* GraphQL query or a search sent as POST. Without it a POST/PATCH is retried
|
|
38
|
+
* only on a 429, never on a 5xx or a dropped connection, because those do not
|
|
39
|
+
* say whether the server already acted on it (a sent email, a created issue).
|
|
40
|
+
*/
|
|
41
|
+
idempotent?: boolean;
|
|
42
|
+
}
|
|
33
43
|
export interface CreateHttpClientOptions {
|
|
34
44
|
/**
|
|
35
45
|
* Static bearer token shorthand: sent as `Authorization: Bearer <token>`
|
|
@@ -56,18 +66,19 @@ export interface HttpClient {
|
|
|
56
66
|
/**
|
|
57
67
|
* Fetch with auth + retry, resolving with the raw `Response` for non-2xx
|
|
58
68
|
* statuses so callers can keep their own `response.ok` handling. Transient
|
|
59
|
-
* statuses (429
|
|
60
|
-
* as a thrown `HttpStatusError` once retries are
|
|
69
|
+
* statuses (408, 429, 5xx — see `classifyToolError`) still throw to drive the
|
|
70
|
+
* retry loop, so they surface as a thrown `HttpStatusError` once retries are
|
|
71
|
+
* exhausted.
|
|
61
72
|
*/
|
|
62
|
-
raw(url: string, init?:
|
|
73
|
+
raw(url: string, init?: HttpRequestInit): Promise<Response>;
|
|
63
74
|
/** Fetch with auth + retry; throws `HttpStatusError` on any non-2xx status. */
|
|
64
|
-
request(url: string, init?:
|
|
75
|
+
request(url: string, init?: HttpRequestInit): Promise<Response>;
|
|
65
76
|
/** `request()` + parse the response body as JSON (any method via `init`). */
|
|
66
|
-
json<T>(url: string, init?:
|
|
77
|
+
json<T>(url: string, init?: HttpRequestInit): Promise<T>;
|
|
67
78
|
/** GET returning parsed JSON. */
|
|
68
|
-
get<T>(url: string, init?:
|
|
79
|
+
get<T>(url: string, init?: HttpRequestInit): Promise<T>;
|
|
69
80
|
/** POST `body` as JSON (unless a Content-Type is supplied) returning parsed JSON. */
|
|
70
|
-
post<T>(url: string, body?: unknown, init?:
|
|
81
|
+
post<T>(url: string, body?: unknown, init?: HttpRequestInit): Promise<T>;
|
|
71
82
|
}
|
|
72
83
|
export declare function createHttpClient(options?: CreateHttpClientOptions): HttpClient;
|
|
73
84
|
export interface RequireBearerClientOptions {
|
|
@@ -7,14 +7,10 @@
|
|
|
7
7
|
* `Retry-After`, and descriptive errors that include the status code and a
|
|
8
8
|
* truncated response body.
|
|
9
9
|
*/
|
|
10
|
-
import { withHttpRetry } from './retry.js';
|
|
10
|
+
import { isTransientStatus, withHttpRetry } from './retry.js';
|
|
11
11
|
import { sleep } from './sleep.js';
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
* these so `withHttpRetry` can retry them (its keyword classifier recognizes
|
|
15
|
-
* the status code embedded in the message).
|
|
16
|
-
*/
|
|
17
|
-
const TRANSIENT_STATUSES = new Set([429, 500, 502, 503, 504]);
|
|
12
|
+
/** Methods RFC 9110 §9.2.2 defines as idempotent: repeating one has the effect of sending it once. */
|
|
13
|
+
const IDEMPOTENT_METHODS = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE', 'PUT', 'DELETE']);
|
|
18
14
|
const BODY_PREVIEW_CHARS = 500;
|
|
19
15
|
const DEFAULT_MAX_RETRY_AFTER_MS = 30_000;
|
|
20
16
|
/** Error thrown for non-2xx responses. Message: `<prefix> <METHOD> <url> failed (<status>): <truncated body>`. */
|
|
@@ -85,10 +81,11 @@ export function createHttpClient(options = {}) {
|
|
|
85
81
|
async function attempt(url, init, throwOnAnyError) {
|
|
86
82
|
const method = (init?.method ?? 'GET').toUpperCase();
|
|
87
83
|
const headers = await buildHeaders(init);
|
|
88
|
-
const
|
|
84
|
+
const { idempotent: _idempotent, ...fetchInit } = init ?? {};
|
|
85
|
+
const response = await fetch(url, { ...fetchInit, headers });
|
|
89
86
|
if (response.ok)
|
|
90
87
|
return response;
|
|
91
|
-
if (
|
|
88
|
+
if (isTransientStatus(response.status)) {
|
|
92
89
|
const error = await statusError(response, method, url);
|
|
93
90
|
if (response.status === 429 && retryEnabled) {
|
|
94
91
|
const retryAfterMs = parseRetryAfterMs(response.headers.get('Retry-After'));
|
|
@@ -101,13 +98,18 @@ export function createHttpClient(options = {}) {
|
|
|
101
98
|
throw await statusError(response, method, url);
|
|
102
99
|
return response;
|
|
103
100
|
}
|
|
104
|
-
function withRetry(fn) {
|
|
101
|
+
function withRetry(init, fn) {
|
|
105
102
|
if (!retryEnabled)
|
|
106
103
|
return fn();
|
|
107
|
-
|
|
104
|
+
const method = (init?.method ?? 'GET').toUpperCase();
|
|
105
|
+
return withHttpRetry(fn, {
|
|
106
|
+
operation: `${errorPrefix} request`,
|
|
107
|
+
...retryOptions,
|
|
108
|
+
repeatable: IDEMPOTENT_METHODS.has(method) || init?.idempotent === true,
|
|
109
|
+
});
|
|
108
110
|
}
|
|
109
|
-
const raw = (url, init) => withRetry(() => attempt(url, init, false));
|
|
110
|
-
const request = (url, init) => withRetry(() => attempt(url, init, true));
|
|
111
|
+
const raw = (url, init) => withRetry(init, () => attempt(url, init, false));
|
|
112
|
+
const request = (url, init) => withRetry(init, () => attempt(url, init, true));
|
|
111
113
|
const json = async (url, init) => {
|
|
112
114
|
const response = await request(url, init);
|
|
113
115
|
return (await response.json());
|
|
@@ -23,7 +23,7 @@ export type { AccessIdentitySpec, AccessMember, AccessResource, AclSourceDef, Ch
|
|
|
23
23
|
export { ACL_RESOURCE_TYPE, ACL_RESOURCE_TYPE_SLUG, } from './acl-source.js';
|
|
24
24
|
import { EVENT_RECALL_IDENTITY_NAMESPACES, getIdentityNamespaceDefinition, IDENTITY, IDENTITY_NAMESPACE_REGISTRY, isEventRecallIdentityNamespace } from './identity-namespaces.js';
|
|
25
25
|
export { EVENT_RECALL_IDENTITY_NAMESPACES, getIdentityNamespaceDefinition, IDENTITY, IDENTITY_NAMESPACE_REGISTRY, isEventRecallIdentityNamespace, };
|
|
26
|
-
export type { CreateHttpClientOptions, HttpClient, RequireBearerClientOptions, } from './http-client.js';
|
|
26
|
+
export type { CreateHttpClientOptions, HttpClient, HttpRequestInit, RequireBearerClientOptions, } from './http-client.js';
|
|
27
27
|
export { createHttpClient, HttpStatusError, requireBearerClient } from './http-client.js';
|
|
28
28
|
export { sdkLogger, sdkLogger as logger } from './logger.js';
|
|
29
29
|
export type { CursorPage, OffsetPage, PaginateByCursorOptions, PaginateByOffsetOptions, } from './pagination.js';
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* HTTP retry helper for connector SDK.
|
|
3
3
|
*
|
|
4
|
-
* Exponential backoff with full jitter (5 retries, 1s → 16s)
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Exponential backoff with full jitter (5 retries, 1s → 16s). Whether an error
|
|
5
|
+
* is worth retrying is decided by `classifyToolError`, the one catalog shared
|
|
6
|
+
* with the server: an HTTP status or SQLSTATE the error carries always wins,
|
|
7
|
+
* and message text is consulted only when it carries neither. A response
|
|
8
|
+
* body or URL is never evidence — a 503 whose body says "invalid" is still a
|
|
9
|
+
* 503.
|
|
7
10
|
*
|
|
8
11
|
* The backoff loop lives here rather than being imported from `@lobu/core`:
|
|
9
12
|
* core's root entry drags winston, Sentry and OpenTelemetry into every
|
|
@@ -11,10 +14,19 @@
|
|
|
11
14
|
* isolate. It keeps the semantics of core's `retryWithBackoff` for the one
|
|
12
15
|
* configuration `withHttpRetry` uses: exponential, capped, full jitter.
|
|
13
16
|
*/
|
|
17
|
+
/** Whether a response with this HTTP status may succeed if the request is repeated. */
|
|
18
|
+
export declare function isTransientStatus(status: number): boolean;
|
|
14
19
|
interface RetryOptions {
|
|
15
20
|
operation?: string;
|
|
16
21
|
context?: Record<string, any>;
|
|
17
22
|
onRetry?: (error: Error, attempt: number) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Whether repeating the call is safe when an earlier attempt may already have
|
|
25
|
+
* taken effect. Default `true`. Pass `false` for a write (a send, a create): it
|
|
26
|
+
* is then retried only on a 429, which proves the server did not act on it —
|
|
27
|
+
* never on a 5xx or a dropped connection (RFC 9110 §9.2.2).
|
|
28
|
+
*/
|
|
29
|
+
repeatable?: boolean;
|
|
18
30
|
}
|
|
19
31
|
/**
|
|
20
32
|
* HTTP retry strategy
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* HTTP retry helper for connector SDK.
|
|
3
3
|
*
|
|
4
|
-
* Exponential backoff with full jitter (5 retries, 1s → 16s)
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Exponential backoff with full jitter (5 retries, 1s → 16s). Whether an error
|
|
5
|
+
* is worth retrying is decided by `classifyToolError`, the one catalog shared
|
|
6
|
+
* with the server: an HTTP status or SQLSTATE the error carries always wins,
|
|
7
|
+
* and message text is consulted only when it carries neither. A response
|
|
8
|
+
* body or URL is never evidence — a 503 whose body says "invalid" is still a
|
|
9
|
+
* 503.
|
|
7
10
|
*
|
|
8
11
|
* The backoff loop lives here rather than being imported from `@lobu/core`:
|
|
9
12
|
* core's root entry drags winston, Sentry and OpenTelemetry into every
|
|
@@ -11,6 +14,7 @@
|
|
|
11
14
|
* isolate. It keeps the semantics of core's `retryWithBackoff` for the one
|
|
12
15
|
* configuration `withHttpRetry` uses: exponential, capped, full jitter.
|
|
13
16
|
*/
|
|
17
|
+
import { classifyToolError, isRetryable, } from '@lobu/core/connector-query-errors';
|
|
14
18
|
import { sdkLogger } from './logger.js';
|
|
15
19
|
async function retryWithBackoff(fn, options) {
|
|
16
20
|
const { maxRetries, baseDelay, maxDelay, shouldRetry, onRetry } = options;
|
|
@@ -41,61 +45,38 @@ async function retryWithBackoff(fn, options) {
|
|
|
41
45
|
}
|
|
42
46
|
throw lastError;
|
|
43
47
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
'
|
|
76
|
-
'too many requests',
|
|
77
|
-
// server
|
|
78
|
-
'500',
|
|
79
|
-
'502',
|
|
80
|
-
'503',
|
|
81
|
-
'504',
|
|
82
|
-
'server error',
|
|
83
|
-
'service unavailable',
|
|
84
|
-
'gateway timeout',
|
|
85
|
-
];
|
|
86
|
-
const PERMANENT_KEYWORDS = [
|
|
87
|
-
'not found',
|
|
88
|
-
'404',
|
|
89
|
-
'unauthorized',
|
|
90
|
-
'401',
|
|
91
|
-
'forbidden',
|
|
92
|
-
'403',
|
|
93
|
-
'invalid',
|
|
94
|
-
'bad request',
|
|
95
|
-
'400',
|
|
96
|
-
];
|
|
97
|
-
function errorMessage(error) {
|
|
98
|
-
return (error instanceof Error ? error.message : String(error)).toLowerCase();
|
|
48
|
+
/**
|
|
49
|
+
* The structured signal an error carries, read off the error itself. An
|
|
50
|
+
* `HttpStatusError` carries `status`; a postgres.js error carries a SQLSTATE
|
|
51
|
+
* `code`. The message rides along only as the catalog's last resort, for
|
|
52
|
+
* errors that arrive as text alone (a failed fetch inside the isolate is a
|
|
53
|
+
* bare `TypeError('fetch failed: …')`).
|
|
54
|
+
*/
|
|
55
|
+
function errorSignal(error) {
|
|
56
|
+
if (!(error instanceof Error))
|
|
57
|
+
return { message: String(error) };
|
|
58
|
+
const fields = error;
|
|
59
|
+
return {
|
|
60
|
+
httpStatus: typeof fields.status === 'number' ? fields.status : undefined,
|
|
61
|
+
pgCode: typeof fields.code === 'string' ? fields.code : undefined,
|
|
62
|
+
message: error.message,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Whether a response with this HTTP status may succeed if the request is repeated. */
|
|
66
|
+
export function isTransientStatus(status) {
|
|
67
|
+
return isRetryable(classifyToolError({ httpStatus: status }));
|
|
68
|
+
}
|
|
69
|
+
/** Whether `error` may succeed if the identical call is repeated. */
|
|
70
|
+
function isTransientError(error) {
|
|
71
|
+
return isRetryable(classifyToolError(errorSignal(error)));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether `error` proves the server refused the request without acting on it.
|
|
75
|
+
* Only a 429 says so; a 5xx or a dropped connection says nothing about whether
|
|
76
|
+
* a write landed.
|
|
77
|
+
*/
|
|
78
|
+
function isUnprocessed(error) {
|
|
79
|
+
return classifyToolError(errorSignal(error)) === 'RATE_LIMITED';
|
|
99
80
|
}
|
|
100
81
|
/**
|
|
101
82
|
* HTTP retry strategy
|
|
@@ -110,12 +91,7 @@ export async function withHttpRetry(fn, options) {
|
|
|
110
91
|
maxRetries: totalRetries,
|
|
111
92
|
baseDelay: 1000,
|
|
112
93
|
maxDelay: 16000,
|
|
113
|
-
shouldRetry:
|
|
114
|
-
const msg = errorMessage(error);
|
|
115
|
-
if (PERMANENT_KEYWORDS.some((k) => msg.includes(k)))
|
|
116
|
-
return false;
|
|
117
|
-
return TRANSIENT_KEYWORDS.some((k) => msg.includes(k));
|
|
118
|
-
},
|
|
94
|
+
shouldRetry: options?.repeatable === false ? isUnprocessed : isTransientError,
|
|
119
95
|
onRetry: (attempt, error) => {
|
|
120
96
|
options?.onRetry?.(error, attempt);
|
|
121
97
|
sdkLogger.debug({
|
|
@@ -341,6 +341,53 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
341
341
|
let toolCalls = 0;
|
|
342
342
|
/** Insertion-ordered, so the ledger reads in first-call order. */
|
|
343
343
|
const toolsUsed = new Set();
|
|
344
|
+
/**
|
|
345
|
+
* Per-input attribution for the tools above. The owner input's own calls
|
|
346
|
+
* stay in `toolsUsed`; each steered input run id owns its own ledger. A
|
|
347
|
+
* terminal reply is stamped with its input's ledger only, never the
|
|
348
|
+
* execution-wide union — otherwise a sibling input's calls would be reported
|
|
349
|
+
* as this reply's evidence (#3662).
|
|
350
|
+
*/
|
|
351
|
+
const steeredToolsUsed = new Map();
|
|
352
|
+
/**
|
|
353
|
+
* First tool error per input, quoted exactly as the tool returned it. Bound
|
|
354
|
+
* at call START to the input the call was made for, so a result that lands
|
|
355
|
+
* after a mid-turn follow-up stays with its initiating message.
|
|
356
|
+
*/
|
|
357
|
+
let ownerFirstError;
|
|
358
|
+
const steeredFirstErrors = new Map();
|
|
359
|
+
/** Accepted input run id the active tool call was started for (`undefined` = owner). */
|
|
360
|
+
let activeInputRunId;
|
|
361
|
+
/**
|
|
362
|
+
* The input that emitted the current tool batch: captured on every assistant
|
|
363
|
+
* `message_end` (the message that carries the calls), because a steered
|
|
364
|
+
* follow-up can be appended — moving `activeInputRunId` — between the model
|
|
365
|
+
* emitting a call and the worker starting or finishing it. The result stays
|
|
366
|
+
* with the message that initiated it, never with whichever input is active
|
|
367
|
+
* when it lands.
|
|
368
|
+
*/
|
|
369
|
+
let emitInputRunId;
|
|
370
|
+
const toolInputRun = new Map();
|
|
371
|
+
const toolsFor = (runId) => {
|
|
372
|
+
if (runId === undefined)
|
|
373
|
+
return toolsUsed;
|
|
374
|
+
let set = steeredToolsUsed.get(runId);
|
|
375
|
+
if (!set) {
|
|
376
|
+
set = new Set();
|
|
377
|
+
steeredToolsUsed.set(runId, set);
|
|
378
|
+
}
|
|
379
|
+
return set;
|
|
380
|
+
};
|
|
381
|
+
const noteToolError = (runId, output) => {
|
|
382
|
+
if (!output)
|
|
383
|
+
return;
|
|
384
|
+
if (runId === undefined) {
|
|
385
|
+
ownerFirstError ??= output;
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (!steeredFirstErrors.has(runId))
|
|
389
|
+
steeredFirstErrors.set(runId, output);
|
|
390
|
+
};
|
|
344
391
|
// `ask_user` hands the conversation back to the human: the question is posted
|
|
345
392
|
// as buttons and the click returns as a NEW inbound message, which is a new
|
|
346
393
|
// turn. Stop the session at that point, or the model keeps calling tools
|
|
@@ -449,6 +496,9 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
449
496
|
// turn, in order. It drives the live typing indicator and is the fallback
|
|
450
497
|
// answer for a turn that never settles a message.
|
|
451
498
|
let text = '';
|
|
499
|
+
// Owner-only fallback stream. The aggregate `text` includes steered
|
|
500
|
+
// replies too, so it cannot safely stand in for an unsettled owner answer.
|
|
501
|
+
let ownerText = '';
|
|
452
502
|
let stopReason = null;
|
|
453
503
|
let usage = null;
|
|
454
504
|
// While the pre-compaction memory flush runs, nothing it produces is the
|
|
@@ -458,12 +508,29 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
458
508
|
// arrived at exactly those points — after an assistant message, after a tool
|
|
459
509
|
// result — and queue it as the user message it is.
|
|
460
510
|
const steeredMessages = new Map();
|
|
511
|
+
// Steered input run ids in arrival order. A user `message_end` closes the
|
|
512
|
+
// answer to the message before it: the first one closes the owner's, each
|
|
513
|
+
// later one hands the active input to the next steered run. FIFO, not
|
|
514
|
+
// identity: pi rewrites messages on the way to the provider, so the event
|
|
515
|
+
// object is not reliably the one `agent.steer` was handed (see the
|
|
516
|
+
// `messageContext` note above).
|
|
517
|
+
const steeredRunOrder = [];
|
|
461
518
|
// The turn's answer(s), one per user message the model replied to. Tracked
|
|
462
519
|
// from events rather than read back out of `agent.state.messages` at the
|
|
463
520
|
// end: compaction replaces that array mid-turn, and the newest message is
|
|
464
521
|
// the wrong one to read anyway — see the `text` field of the result.
|
|
465
|
-
|
|
522
|
+
let ownerAnswer = null;
|
|
523
|
+
const inputAnswers = new Map();
|
|
524
|
+
let sawInitialUser = false;
|
|
466
525
|
let answer = null;
|
|
526
|
+
const settleAnswer = () => {
|
|
527
|
+
if (!answer)
|
|
528
|
+
return;
|
|
529
|
+
if (activeInputRunId === undefined)
|
|
530
|
+
ownerAnswer = answer;
|
|
531
|
+
else
|
|
532
|
+
inputAnswers.set(activeInputRunId, answer);
|
|
533
|
+
};
|
|
467
534
|
const steer = () => {
|
|
468
535
|
if (flushing)
|
|
469
536
|
return;
|
|
@@ -472,6 +539,7 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
472
539
|
role: 'user', content: [{ type: 'text', text: message.text }], timestamp: Date.now(),
|
|
473
540
|
};
|
|
474
541
|
steeredMessages.set(nativeMessage, message.runId);
|
|
542
|
+
steeredRunOrder.push(message.runId);
|
|
475
543
|
// Its own block, beside its own message. Recorded BEFORE the steer so
|
|
476
544
|
// the loader can never see the message without its context.
|
|
477
545
|
// Last writer wins on identical text. Two follow-ups with the SAME
|
|
@@ -501,6 +569,8 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
501
569
|
const partial = event.assistantMessageEvent;
|
|
502
570
|
if (partial.type === 'text_delta' && typeof partial.delta === 'string') {
|
|
503
571
|
text += partial.delta;
|
|
572
|
+
if (activeInputRunId === undefined)
|
|
573
|
+
ownerText += partial.delta;
|
|
504
574
|
emit({ type: 'text_delta', delta: partial.delta });
|
|
505
575
|
}
|
|
506
576
|
else if (partial.type === 'thinking_delta' && typeof partial.delta === 'string') {
|
|
@@ -513,8 +583,11 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
513
583
|
// A user message — the prompt, or a steered follow-up Pi injected —
|
|
514
584
|
// closes the answer to the message before it.
|
|
515
585
|
if (message.role === 'user') {
|
|
516
|
-
|
|
517
|
-
|
|
586
|
+
settleAnswer();
|
|
587
|
+
if (sawInitialUser)
|
|
588
|
+
activeInputRunId = steeredRunOrder.shift();
|
|
589
|
+
else
|
|
590
|
+
sawInitialUser = true;
|
|
518
591
|
answer = null;
|
|
519
592
|
return;
|
|
520
593
|
}
|
|
@@ -522,6 +595,11 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
522
595
|
// turn's outcome.
|
|
523
596
|
if (message.role !== 'assistant')
|
|
524
597
|
return;
|
|
598
|
+
// The calls this message emitted belong to the input it is answering,
|
|
599
|
+
// whatever steers the turn before they run. Captured here rather than
|
|
600
|
+
// at call start: `steer()` above may already have queued a follow-up
|
|
601
|
+
// whose user message lands before the first call starts.
|
|
602
|
+
emitInputRunId = activeInputRunId;
|
|
525
603
|
// The LAST assistant message with text answers its user message: a
|
|
526
604
|
// narration before a tool call is superseded by the message that
|
|
527
605
|
// follows the result. An EMPTY one (an aborted or errored request)
|
|
@@ -541,6 +619,13 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
541
619
|
return;
|
|
542
620
|
}
|
|
543
621
|
if (event.type === 'tool_execution_start') {
|
|
622
|
+
// Bound now, read at END: a call emitted for one input may start and
|
|
623
|
+
// finish after a follow-up steered the turn elsewhere, and the result
|
|
624
|
+
// belongs to the message that initiated it. The binding is the input
|
|
625
|
+
// of the assistant message that emitted the call, not the live input.
|
|
626
|
+
// `undefined` is a real owner binding, not a missing value. Preserve
|
|
627
|
+
// it so a later steer cannot claim an owner tool that starts late.
|
|
628
|
+
toolInputRun.set(event.toolCallId, emitInputRunId);
|
|
544
629
|
emit({ type: 'tool_call_start', toolCallId: event.toolCallId, name: event.toolName, args: event.args });
|
|
545
630
|
return;
|
|
546
631
|
}
|
|
@@ -549,18 +634,29 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
549
634
|
// matching what the retired lane stamped (`recordToolUsed`, from its
|
|
550
635
|
// own `tool_use` event). A failed call still counts as attempted; the
|
|
551
636
|
// guardrail asks whether the tool was reached, not whether it worked.
|
|
552
|
-
|
|
637
|
+
const inputRunId = toolInputRun.has(event.toolCallId)
|
|
638
|
+
? toolInputRun.get(event.toolCallId)
|
|
639
|
+
: activeInputRunId;
|
|
640
|
+
toolInputRun.delete(event.toolCallId);
|
|
641
|
+
toolsFor(inputRunId).add(event.toolName);
|
|
553
642
|
const result = event.result;
|
|
554
643
|
// Summarised BEFORE the clip, from the result as the tool returned it:
|
|
555
644
|
// a retrieval body over the display cap would otherwise parse to
|
|
556
645
|
// nothing and the turn would carry no evidence for its own answer.
|
|
557
646
|
const resultSummary = event.isError ? null : summarizeToolTrace(event.toolName, event.result);
|
|
647
|
+
const output = clip(joinText(result?.content));
|
|
648
|
+
// The first failure of each input, verbatim: the durable record of
|
|
649
|
+
// what actually went wrong, independent of how the model paraphrases
|
|
650
|
+
// it in prose.
|
|
651
|
+
if (event.isError)
|
|
652
|
+
noteToolError(inputRunId, output);
|
|
558
653
|
emit({
|
|
559
654
|
type: 'tool_call_end',
|
|
560
655
|
toolCallId: event.toolCallId,
|
|
561
656
|
name: event.toolName,
|
|
562
657
|
isError: event.isError,
|
|
563
|
-
output
|
|
658
|
+
output,
|
|
659
|
+
...(inputRunId !== undefined ? { inputRunId } : {}),
|
|
564
660
|
...(resultSummary ? { resultSummary } : {}),
|
|
565
661
|
});
|
|
566
662
|
}
|
|
@@ -633,8 +729,7 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
633
729
|
}
|
|
634
730
|
if (ended)
|
|
635
731
|
throw new Error(ended);
|
|
636
|
-
|
|
637
|
-
answers.push(answer);
|
|
732
|
+
settleAnswer();
|
|
638
733
|
return {
|
|
639
734
|
// The answer is the last assistant message WITH TEXT after each user
|
|
640
735
|
// message, not the accumulated stream: a model that narrates before a
|
|
@@ -654,17 +749,34 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
|
|
|
654
749
|
// turn on one — so the answer that did settle is still delivered.
|
|
655
750
|
//
|
|
656
751
|
// Falls back to the stream when no assistant message settled: a turn
|
|
657
|
-
// aborted mid-answer still owes the user what it managed to say.
|
|
658
|
-
|
|
752
|
+
// aborted mid-answer still owes the user what it managed to say. The
|
|
753
|
+
// fallback is the owner's only: a steered input without its own settled
|
|
754
|
+
// answer fails below rather than borrowing a sibling's text.
|
|
755
|
+
text: ownerAnswer ?? ownerText,
|
|
659
756
|
stopReason,
|
|
660
757
|
usage,
|
|
661
758
|
sessionJsonl: nativeSessionJsonl(session),
|
|
662
759
|
toolsUsed: [...toolsUsed],
|
|
760
|
+
...(ownerFirstError ? { firstError: ownerFirstError } : {}),
|
|
663
761
|
// Pi emits message events before persistence. Receipt identity comes from
|
|
664
762
|
// the finished native branch after retries/compaction have drained.
|
|
665
763
|
consumedInputs: session.sessionManager.getBranch().flatMap((entry) => {
|
|
666
764
|
const runId = entry.type === 'message' ? steeredMessages.get(entry.message) : undefined;
|
|
667
|
-
|
|
765
|
+
if (runId === undefined)
|
|
766
|
+
return [];
|
|
767
|
+
// Fail closed: a steered input the turn never answered must not be
|
|
768
|
+
// completed with another input's text. The server turns this throw
|
|
769
|
+
// into a failed turn, which the reaper then terminalizes honestly.
|
|
770
|
+
const responseText = inputAnswers.get(runId);
|
|
771
|
+
if (!responseText?.trim())
|
|
772
|
+
throw new Error(`steered input ${runId} completed without its own answer`);
|
|
773
|
+
return [{
|
|
774
|
+
runId,
|
|
775
|
+
sessionEntryId: entry.id,
|
|
776
|
+
responseText,
|
|
777
|
+
toolsUsed: [...(steeredToolsUsed.get(runId) ?? [])],
|
|
778
|
+
...(steeredFirstErrors.get(runId) ? { firstError: steeredFirstErrors.get(runId) } : {}),
|
|
779
|
+
}];
|
|
668
780
|
}),
|
|
669
781
|
...(repliedInBand ? { repliedInBand: true } : {}),
|
|
670
782
|
};
|