actorctl 0.3.0-rc.9 → 0.3.1-rc.23
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/actorctl.js +254 -66
- package/dist/user-worker.js +116 -156
- package/dist/worker/models.ts +50 -11
- package/package.json +2 -2
package/dist/actorctl.js
CHANGED
|
@@ -21214,6 +21214,10 @@ var codeByLiteral = {
|
|
|
21214
21214
|
NetworkAuthenticationRequired: 511
|
|
21215
21215
|
};
|
|
21216
21216
|
var fromLiteral = (literal) => codeByLiteral[literal];
|
|
21217
|
+
// src/chatgpt.ts
|
|
21218
|
+
import { createServer } from "http";
|
|
21219
|
+
import { createInterface as createInterface2 } from "readline";
|
|
21220
|
+
|
|
21217
21221
|
// ../../node_modules/.bun/effect@4.1.2/node_modules/effect/dist/encoding/Sse.js
|
|
21218
21222
|
var SseErrorTypeId = "~effect/encoding/Sse/SseError";
|
|
21219
21223
|
|
|
@@ -26067,10 +26071,59 @@ class ArtifactStoreError extends TaggedError2("ArtifactStoreError") {
|
|
|
26067
26071
|
class ArtifactStore extends Service()("tardigrade-cloud/ArtifactStore") {
|
|
26068
26072
|
}
|
|
26069
26073
|
|
|
26074
|
+
// src/worker/webhooks.ts
|
|
26075
|
+
var CallMetadata = Record(String5, String5.check(isMaxLength(512))).check(makeFilter2((value) => Object.keys(value).length <= 16 && Object.keys(value).every((key) => key.length <= 64) || "Metadata accepts at most 16 pairs with keys up to 64 characters."));
|
|
26076
|
+
var ReplyAddress = Struct({
|
|
26077
|
+
webhook: NonEmptyString,
|
|
26078
|
+
account: NonEmptyString,
|
|
26079
|
+
actor: NonEmptyString,
|
|
26080
|
+
thread: NonEmptyString,
|
|
26081
|
+
method: NonEmptyString,
|
|
26082
|
+
id: NonEmptyString,
|
|
26083
|
+
metadata: CallMetadata,
|
|
26084
|
+
url: String5
|
|
26085
|
+
});
|
|
26086
|
+
|
|
26087
|
+
// ../../src/services/webhooks.ts
|
|
26088
|
+
var WebhookEventType = Literals(["call.completed", "call.failed", "call.cancelled", "webhook.test"]);
|
|
26089
|
+
var WebhookEndpoint = Struct({
|
|
26090
|
+
id: String5,
|
|
26091
|
+
url: String5,
|
|
26092
|
+
events: ArraySchema(WebhookEventType),
|
|
26093
|
+
description: String5,
|
|
26094
|
+
status: Literals(["active", "disabled"]),
|
|
26095
|
+
createdAt: String5
|
|
26096
|
+
});
|
|
26097
|
+
var WebhookCreate = Struct({ url: String5, events: optionalKey2(ArraySchema(WebhookEventType)), description: optionalKey2(String5) });
|
|
26098
|
+
var WebhookPatch = Struct({
|
|
26099
|
+
url: optionalKey2(String5),
|
|
26100
|
+
events: optionalKey2(ArraySchema(WebhookEventType)),
|
|
26101
|
+
description: optionalKey2(String5),
|
|
26102
|
+
status: optionalKey2(Literals(["active", "disabled"]))
|
|
26103
|
+
});
|
|
26104
|
+
|
|
26105
|
+
class WebhookError extends TaggedError2("WebhookError") {
|
|
26106
|
+
}
|
|
26107
|
+
|
|
26108
|
+
class WebhookStore extends Service()("tardigrade-cloud/WebhookStore") {
|
|
26109
|
+
}
|
|
26110
|
+
var WebhookAttempt = Struct({ startedAt: Number6, finishedAt: NullOr(Number6), statusCode: NullOr(Number6), error: NullOr(String5) });
|
|
26111
|
+
var WebhookDelivery = Struct({
|
|
26112
|
+
id: String5,
|
|
26113
|
+
type: WebhookEventType,
|
|
26114
|
+
status: Literals(["pending", "delivered", "failed", "skipped"]),
|
|
26115
|
+
createdAt: Number6,
|
|
26116
|
+
attempts: ArraySchema(WebhookAttempt)
|
|
26117
|
+
});
|
|
26118
|
+
|
|
26119
|
+
class WebhookDeliveryService extends Service()("tardigrade-cloud/WebhookDeliveryService") {
|
|
26120
|
+
}
|
|
26121
|
+
|
|
26070
26122
|
// ../../src/services/metadata-store.ts
|
|
26071
26123
|
var Actor2 = Struct({
|
|
26072
26124
|
name: NonEmptyString,
|
|
26073
|
-
description: String5
|
|
26125
|
+
description: String5,
|
|
26126
|
+
metadata: optionalKey2(CallMetadata)
|
|
26074
26127
|
}).annotate({ identifier: "Actor" });
|
|
26075
26128
|
var Version2 = Struct({
|
|
26076
26129
|
id: NonEmptyString,
|
|
@@ -26118,9 +26171,9 @@ var Credits = Struct({
|
|
|
26118
26171
|
entries: ArraySchema(CreditEntry),
|
|
26119
26172
|
cursor: optionalKey2(NonEmptyString)
|
|
26120
26173
|
}).annotate({ identifier: "Credits" });
|
|
26121
|
-
var UsageGroup = Literals(["day", "model", "actor"]);
|
|
26174
|
+
var UsageGroup = Union2([Literals(["day", "model", "actor", "provider"]), String5.check(isPattern2(/^metadata\.[\s\S]{1,64}$/))]);
|
|
26122
26175
|
var UsageRow = Struct({
|
|
26123
|
-
key: String5,
|
|
26176
|
+
key: NullOr(String5),
|
|
26124
26177
|
requests: Int,
|
|
26125
26178
|
inputTokens: Int,
|
|
26126
26179
|
outputTokens: Int,
|
|
@@ -26146,54 +26199,22 @@ var CreditGrant = Struct({
|
|
|
26146
26199
|
var InsufficientCredits = Struct({
|
|
26147
26200
|
error: Struct({ type: Literal2("insufficient_credits"), message: String5, balanceMicros: Micros })
|
|
26148
26201
|
}).annotate({ identifier: "InsufficientCredits" });
|
|
26149
|
-
// src/worker/webhooks.ts
|
|
26150
|
-
var CallMetadata = Record(String5, String5.check(isMaxLength(512))).check(makeFilter2((value) => Object.keys(value).length <= 16 && Object.keys(value).every((key) => key.length <= 64) || "Metadata accepts at most 16 pairs with keys up to 64 characters."));
|
|
26151
|
-
var ReplyAddress = Struct({
|
|
26152
|
-
webhook: NonEmptyString,
|
|
26153
|
-
account: NonEmptyString,
|
|
26154
|
-
actor: NonEmptyString,
|
|
26155
|
-
thread: NonEmptyString,
|
|
26156
|
-
method: NonEmptyString,
|
|
26157
|
-
id: NonEmptyString,
|
|
26158
|
-
metadata: CallMetadata,
|
|
26159
|
-
url: String5
|
|
26160
|
-
});
|
|
26161
|
-
|
|
26162
|
-
// ../../src/services/webhooks.ts
|
|
26163
|
-
var WebhookEventType = Literals(["call.completed", "call.failed", "call.cancelled", "webhook.test"]);
|
|
26164
|
-
var WebhookEndpoint = Struct({
|
|
26165
|
-
id: String5,
|
|
26166
|
-
url: String5,
|
|
26167
|
-
events: ArraySchema(WebhookEventType),
|
|
26168
|
-
description: String5,
|
|
26169
|
-
status: Literals(["active", "disabled"]),
|
|
26170
|
-
createdAt: String5
|
|
26171
|
-
});
|
|
26172
|
-
var WebhookCreate = Struct({ url: String5, events: optionalKey2(ArraySchema(WebhookEventType)), description: optionalKey2(String5) });
|
|
26173
|
-
var WebhookPatch = Struct({
|
|
26174
|
-
url: optionalKey2(String5),
|
|
26175
|
-
events: optionalKey2(ArraySchema(WebhookEventType)),
|
|
26176
|
-
description: optionalKey2(String5),
|
|
26177
|
-
status: optionalKey2(Literals(["active", "disabled"]))
|
|
26178
|
-
});
|
|
26179
|
-
|
|
26180
|
-
class WebhookError extends TaggedError2("WebhookError") {
|
|
26181
|
-
}
|
|
26182
|
-
|
|
26183
|
-
class WebhookStore extends Service()("tardigrade-cloud/WebhookStore") {
|
|
26184
|
-
}
|
|
26185
|
-
var WebhookAttempt = Struct({ startedAt: Number6, finishedAt: NullOr(Number6), statusCode: NullOr(Number6), error: NullOr(String5) });
|
|
26186
|
-
var WebhookDelivery = Struct({
|
|
26187
|
-
id: String5,
|
|
26188
|
-
type: WebhookEventType,
|
|
26189
|
-
status: Literals(["pending", "delivered", "failed", "skipped"]),
|
|
26190
|
-
createdAt: Number6,
|
|
26191
|
-
attempts: ArraySchema(WebhookAttempt)
|
|
26192
|
-
});
|
|
26193
|
-
|
|
26194
|
-
class WebhookDeliveryService extends Service()("tardigrade-cloud/WebhookDeliveryService") {
|
|
26195
|
-
}
|
|
26196
26202
|
|
|
26203
|
+
// ../../src/inference/chatgpt-api.ts
|
|
26204
|
+
var ChatGptName = String5.check(isPattern2(/^[a-z0-9-]{1,64}$/));
|
|
26205
|
+
var ChatGptConnectionInfo = Struct({
|
|
26206
|
+
id: NonEmptyString,
|
|
26207
|
+
name: ChatGptName,
|
|
26208
|
+
label: String5,
|
|
26209
|
+
owner: String5,
|
|
26210
|
+
state: Literals(["connected", "reconnect_required"]),
|
|
26211
|
+
updatedAt: Number6,
|
|
26212
|
+
email: optionalKey2(String5)
|
|
26213
|
+
});
|
|
26214
|
+
var ChatGptStatus = Struct({ available: Boolean3, connections: ArraySchema(ChatGptConnectionInfo) });
|
|
26215
|
+
var ChatGptAuthorization = Struct({ authorizationUrl: String5 });
|
|
26216
|
+
var ChatGptDisconnected = Struct({ revoked: Boolean3, warning: optionalKey2(String5) });
|
|
26217
|
+
var ChatGptConnectCode = Struct({ code: String5, expiresAt: Number6, name: ChatGptName, connectionId: optionalKey2(String5) });
|
|
26197
26218
|
// ../../src/api.ts
|
|
26198
26219
|
var Health = Struct({ status: Literal2("ok") });
|
|
26199
26220
|
var Unauthorized = Struct({ code: Literal2("unauthorized"), message: String5 }).pipe(status(401));
|
|
@@ -26211,7 +26232,8 @@ var base = "/v1/accounts/:account/actors";
|
|
|
26211
26232
|
var actorPath = `${base}/:name`;
|
|
26212
26233
|
var pageQuery = { cursor: optionalKey2(Identifier) };
|
|
26213
26234
|
var ActorMetadata = Struct({
|
|
26214
|
-
description: optionalKey2(String5)
|
|
26235
|
+
description: optionalKey2(String5),
|
|
26236
|
+
metadata: optionalKey2(CallMetadata)
|
|
26215
26237
|
}).annotate({ identifier: "ActorMetadata" });
|
|
26216
26238
|
var ActorInfo = Struct({
|
|
26217
26239
|
...Actor2.fields,
|
|
@@ -26228,7 +26250,7 @@ var Actors = make37("actors").add(get7("getActor", actorPath, {
|
|
|
26228
26250
|
error: errors
|
|
26229
26251
|
}), get7("listActors", base, {
|
|
26230
26252
|
params: accountParams,
|
|
26231
|
-
query: pageQuery,
|
|
26253
|
+
query: { ...pageQuery, metadata: optionalKey2(fromJsonString2(CallMetadata)) },
|
|
26232
26254
|
success: Struct({ items: ArraySchema(Actor2), cursor: optionalKey2(Identifier) }),
|
|
26233
26255
|
error: errors
|
|
26234
26256
|
}), del("deleteActor", actorPath, {
|
|
@@ -26415,7 +26437,7 @@ var Credits2 = make37("credits").add(get7("getCredits", "/v1/accounts/:account/c
|
|
|
26415
26437
|
error: errors
|
|
26416
26438
|
}), get7("getUsage", "/v1/accounts/:account/usage", {
|
|
26417
26439
|
params: accountParams,
|
|
26418
|
-
query: { from: optionalKey2(queryInteger), to: optionalKey2(queryInteger), groupBy: optionalKey2(UsageGroup) },
|
|
26440
|
+
query: { from: optionalKey2(queryInteger), to: optionalKey2(queryInteger), groupBy: optionalKey2(UsageGroup), metadata: optionalKey2(fromJsonString2(CallMetadata)) },
|
|
26419
26441
|
success: Usage,
|
|
26420
26442
|
error: errors
|
|
26421
26443
|
}));
|
|
@@ -26425,7 +26447,24 @@ var Admin = make37("admin").add(get7("listAccounts", "/v1/admin/accounts", { suc
|
|
|
26425
26447
|
success: CreditEntry,
|
|
26426
26448
|
error: errors
|
|
26427
26449
|
}));
|
|
26428
|
-
var
|
|
26450
|
+
var chatGptPath = "/v1/accounts/:account/chatgpt";
|
|
26451
|
+
var ChatGpt = make37("chatgpt").add(get7("status", chatGptPath, { params: accountParams, success: ChatGptStatus, error: errors }), post("issueConnectCode", `${chatGptPath}/connect-codes`, {
|
|
26452
|
+
params: accountParams,
|
|
26453
|
+
payload: Struct({ personalDevelopment: Boolean3, name: optionalKey2(ChatGptName), connectionId: optionalKey2(NonEmptyString), label: optionalKey2(NonEmptyString) }),
|
|
26454
|
+
success: ChatGptConnectCode,
|
|
26455
|
+
error: errors
|
|
26456
|
+
}), post("authorize", `${chatGptPath}/authorize`, {
|
|
26457
|
+
params: accountParams,
|
|
26458
|
+
payload: Struct({ redirectUri: NonEmptyString, personalDevelopment: Boolean3, code: optionalKey2(NonEmptyString), reconnect: optionalKey2(ChatGptName), name: optionalKey2(ChatGptName) }),
|
|
26459
|
+
success: ChatGptAuthorization,
|
|
26460
|
+
error: errors
|
|
26461
|
+
}), post("callback", `${chatGptPath}/callback`, {
|
|
26462
|
+
params: accountParams,
|
|
26463
|
+
payload: Struct({ callbackUrl: NonEmptyString }),
|
|
26464
|
+
success: ChatGptConnectionInfo,
|
|
26465
|
+
error: errors
|
|
26466
|
+
}), patch("rename", `${chatGptPath}/:connection`, { params: { ...accountParams, connection: NonEmptyString }, payload: Struct({ label: NonEmptyString }), success: ChatGptConnectionInfo, error: errors }), del("disconnect", `${chatGptPath}/:connection`, { params: { ...accountParams, connection: NonEmptyString }, success: ChatGptDisconnected, error: errors }));
|
|
26467
|
+
var Api = make35("tardigrade-cloud").add(make37("health").add(get7("healthz", "/healthz", { success: Health })), Actors, Versions, Deployments, Secrets, Runtime, Webhooks, Query, Models, Credits2, ChatGpt, Admin);
|
|
26429
26468
|
|
|
26430
26469
|
// src/credentials.ts
|
|
26431
26470
|
import { chmod as chmod3, mkdir as mkdir2, readFile as readFile3, rm as rm2, writeFile as writeFile3, rename as rename3 } from "fs/promises";
|
|
@@ -26545,6 +26584,140 @@ var print = (json, value, lines) => log(json ? JSON.stringify(value) : lines.joi
|
|
|
26545
26584
|
`));
|
|
26546
26585
|
var next = (cursor) => cursor === undefined ? [] : [`Next page: --cursor ${JSON.stringify(cursor)}`];
|
|
26547
26586
|
|
|
26587
|
+
// ../../src/inference/chatgpt-errors.ts
|
|
26588
|
+
var invalid5 = new Set(["subscription_sharing_user_not_eligible", "subscription_sharing_unsupported_capability", "subscription_sharing_route_not_supported"]);
|
|
26589
|
+
|
|
26590
|
+
// ../../src/inference/chatgpt-plan.ts
|
|
26591
|
+
var CHATGPT_USAGE_URL = "https://chatgpt.com/#settings/Usage";
|
|
26592
|
+
var CHATGPT_PLAN_WARNING = "For personal development or internal benchmark actors only, not released agents or production traffic. Naming a chatgpt/<name> connection in actor code means anyone who calls that actor, or deploys that code to this workspace, uses that account\u2019s ChatGPT plan. Without gateway routing options, requests use API pricing (credits). Tardigrade stores and refreshes your OpenAI credentials on its servers. Plan limits apply; Use gateway.only to prevent paid fallback; gateway.order permits fallback within its routing policy.";
|
|
26593
|
+
var unsupported = new Set([
|
|
26594
|
+
"background",
|
|
26595
|
+
"conversation",
|
|
26596
|
+
"max_output_tokens",
|
|
26597
|
+
"max_tool_calls",
|
|
26598
|
+
"metadata",
|
|
26599
|
+
"moderation",
|
|
26600
|
+
"multi_agent",
|
|
26601
|
+
"prompt",
|
|
26602
|
+
"prompt_cache_retention",
|
|
26603
|
+
"safety_identifier",
|
|
26604
|
+
"temperature",
|
|
26605
|
+
"top_logprobs",
|
|
26606
|
+
"top_p",
|
|
26607
|
+
"truncation",
|
|
26608
|
+
"user",
|
|
26609
|
+
"previous_response_id",
|
|
26610
|
+
"service_tier",
|
|
26611
|
+
"prompt_cache_options"
|
|
26612
|
+
]);
|
|
26613
|
+
|
|
26614
|
+
// src/chatgpt.ts
|
|
26615
|
+
var loopbackCallback = async () => {
|
|
26616
|
+
let state;
|
|
26617
|
+
let resolveCallback;
|
|
26618
|
+
const result = new Promise((resolve) => {
|
|
26619
|
+
resolveCallback = resolve;
|
|
26620
|
+
});
|
|
26621
|
+
const server = createServer((request, response) => {
|
|
26622
|
+
const url = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
26623
|
+
if (url.pathname !== "/auth/callback" || !state || url.searchParams.get("state") !== state) {
|
|
26624
|
+
response.writeHead(400).end("Invalid sign-in callback.");
|
|
26625
|
+
return;
|
|
26626
|
+
}
|
|
26627
|
+
response.writeHead(200, { "content-type": "text/plain", "cache-control": "no-store", "referrer-policy": "no-referrer" }).end("Return to actorctl to finish connecting ChatGPT.");
|
|
26628
|
+
resolveCallback(`${redirectUri}${url.search}`);
|
|
26629
|
+
});
|
|
26630
|
+
await new Promise((resolve, reject) => {
|
|
26631
|
+
server.once("error", reject);
|
|
26632
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
26633
|
+
});
|
|
26634
|
+
const address = server.address();
|
|
26635
|
+
if (!address || typeof address === "string")
|
|
26636
|
+
throw new Error("Could not start a loopback listener.");
|
|
26637
|
+
const redirectUri = `http://127.0.0.1:${address.port}/auth/callback`;
|
|
26638
|
+
let readline;
|
|
26639
|
+
let timer;
|
|
26640
|
+
return {
|
|
26641
|
+
redirectUri,
|
|
26642
|
+
wait: (expectedState) => {
|
|
26643
|
+
state = expectedState;
|
|
26644
|
+
readline = createInterface2({ input: process.stdin, output: process.stderr, terminal: !!process.stdin.isTTY });
|
|
26645
|
+
readline.on("line", (line) => {
|
|
26646
|
+
try {
|
|
26647
|
+
const callback = new URL(line.trim());
|
|
26648
|
+
if (`${callback.origin}${callback.pathname}` !== redirectUri || callback.searchParams.get("state") !== state)
|
|
26649
|
+
return;
|
|
26650
|
+
resolveCallback(callback.toString());
|
|
26651
|
+
} catch {}
|
|
26652
|
+
});
|
|
26653
|
+
return Promise.race([result, new Promise((_, reject) => {
|
|
26654
|
+
timer = setTimeout(() => reject(new Error("ChatGPT sign-in timed out. Run actorctl chatgpt login again.")), 600000);
|
|
26655
|
+
})]);
|
|
26656
|
+
},
|
|
26657
|
+
close: () => {
|
|
26658
|
+
if (timer)
|
|
26659
|
+
clearTimeout(timer);
|
|
26660
|
+
readline?.close();
|
|
26661
|
+
server.closeAllConnections();
|
|
26662
|
+
server.close();
|
|
26663
|
+
}
|
|
26664
|
+
};
|
|
26665
|
+
};
|
|
26666
|
+
var login = make30("login", {
|
|
26667
|
+
...remote,
|
|
26668
|
+
name: optional6("name", "Connection name chosen in console: lowercase letters, digits and dashes."),
|
|
26669
|
+
reconnect: optional6("reconnect", "Reconnect an existing connection by name; keeps that name."),
|
|
26670
|
+
code: optional6("code", "Single-use connect code from workspace settings; expires after 10 minutes. Uses your saved workspace API key or password."),
|
|
26671
|
+
personalDevelopment: Boolean6("personal-development").pipe(withDefault5(false), withDescription3("Acknowledge personal development or internal benchmark actor use.")),
|
|
26672
|
+
browser: Boolean6("browser").pipe(withDefault5(true), withDescription3("Open the authorization URL in a browser."))
|
|
26673
|
+
}, (flags) => gen2(function* () {
|
|
26674
|
+
const config = yield* resolveConfig(flags);
|
|
26675
|
+
yield* error(CHATGPT_PLAN_WARNING);
|
|
26676
|
+
if (!flags.personalDevelopment && isNone2(flags.code)) {
|
|
26677
|
+
if (!process.stdin.isTTY || !(yield* run3(Confirm({ message: "Use this workspace only for personal development and connect your ChatGPT plan?" })))) {
|
|
26678
|
+
return yield* fail7(new Error("Connect only after acknowledging personal development use; pass --personal-development in a noninteractive terminal."));
|
|
26679
|
+
}
|
|
26680
|
+
}
|
|
26681
|
+
const client = yield* cloudClient(config.url);
|
|
26682
|
+
yield* acquireUseRelease2(tryPromise2(loopbackCallback), (listener) => gen2(function* () {
|
|
26683
|
+
const authorization = yield* client.chatgpt.authorize({ params: { account: config.account }, payload: { redirectUri: listener.redirectUri, personalDevelopment: true, ...isSome2(flags.code) ? { code: flags.code.value } : {}, ...isSome2(flags.reconnect) ? { reconnect: flags.reconnect.value } : {}, ...isSome2(flags.name) ? { name: flags.name.value } : {} } });
|
|
26684
|
+
const state = new URL(authorization.authorizationUrl).searchParams.get("state");
|
|
26685
|
+
if (!state)
|
|
26686
|
+
return yield* fail7(new Error("Cloud did not return an authorization state."));
|
|
26687
|
+
const callback = listener.wait(state);
|
|
26688
|
+
yield* error(`Continue with ChatGPT:
|
|
26689
|
+
${authorization.authorizationUrl}
|
|
26690
|
+
|
|
26691
|
+
If the browser cannot reach this machine, paste its full callback URL here and press Enter.`);
|
|
26692
|
+
if (flags.browser)
|
|
26693
|
+
yield* sync2(() => {
|
|
26694
|
+
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? undefined : "xdg-open";
|
|
26695
|
+
if (command && Bun.which(command))
|
|
26696
|
+
Bun.spawn([command, authorization.authorizationUrl], { stdout: "ignore", stderr: "ignore" }).unref();
|
|
26697
|
+
});
|
|
26698
|
+
const callbackUrl = yield* tryPromise2(() => callback);
|
|
26699
|
+
const result = yield* client.chatgpt.callback({ params: { account: config.account }, payload: { callbackUrl } });
|
|
26700
|
+
yield* print(flags.json, result, [`Connected ${result.name} to this workspace. Select options.gateway.only: ["chatgpt/${result.name}"] in managedModels for openai/* models.`, `Manage usage: ${CHATGPT_USAGE_URL}`]);
|
|
26701
|
+
}), (listener) => sync2(() => listener.close()));
|
|
26702
|
+
}).pipe(mapError2(userError))).pipe(withDescription4("Connect a ChatGPT plan using --code from workspace settings and your existing workspace login."));
|
|
26703
|
+
var status2 = make30("status", remote, (flags) => gen2(function* () {
|
|
26704
|
+
const config = yield* resolveConfig(flags);
|
|
26705
|
+
const client = yield* cloudClient(config.url);
|
|
26706
|
+
const result = yield* client.chatgpt.status({ params: { account: config.account } });
|
|
26707
|
+
yield* print(flags.json, result, [
|
|
26708
|
+
...result.connections.length ? result.connections.map((connection) => `chatgpt/${connection.name} ${connection.label} ${connection.owner} ${connection.state}`) : ["No ChatGPT connections."],
|
|
26709
|
+
...!result.available ? ["ChatGPT plan usage is disabled by the operator."] : [],
|
|
26710
|
+
`Manage usage: ${CHATGPT_USAGE_URL}`
|
|
26711
|
+
]);
|
|
26712
|
+
}).pipe(mapError2(userError))).pipe(withDescription4("Show the workspace's ChatGPT plan connection using your existing workspace login."));
|
|
26713
|
+
var logout = make30("logout", { ...remote, connection: String11("connection").pipe(withDescription3("Connection name to disconnect.")) }, (flags) => gen2(function* () {
|
|
26714
|
+
const config = yield* resolveConfig(flags);
|
|
26715
|
+
const client = yield* cloudClient(config.url);
|
|
26716
|
+
const result = yield* client.chatgpt.disconnect({ params: { account: config.account, connection: (yield* client.chatgpt.status({ params: { account: config.account } })).connections.find((connection) => connection.name === flags.connection)?.id ?? flags.connection } });
|
|
26717
|
+
yield* print(flags.json, result, [`Disconnected ${flags.connection}; code still naming this connection will fail closed.`, ...result.warning ? [result.warning] : []]);
|
|
26718
|
+
}).pipe(mapError2(userError))).pipe(withDescription4("Disconnect as a WorkOS owner/admin; with a workspace API key, use Disconnect in console settings."));
|
|
26719
|
+
var chatgpt = make30("chatgpt").pipe(withSubcommands([login, status2, logout]));
|
|
26720
|
+
|
|
26548
26721
|
// src/runtime.ts
|
|
26549
26722
|
var DEFAULT_EVENT_POLL_INTERVAL_MS = 1000;
|
|
26550
26723
|
var nonempty = (name) => String11(name).pipe(withSchema2(NonEmptyString));
|
|
@@ -26854,13 +27027,15 @@ import { modelActs, modelInfo, toolActs } from "tardie/agent/services"
|
|
|
26854
27027
|
import { weather } from "./actor"
|
|
26855
27028
|
import { managedModels, type ModelBinding } from "./tardigrade/models"
|
|
26856
27029
|
|
|
26857
|
-
//
|
|
27030
|
+
// Models use API pricing (workspace credits) by default. Set options.gateway.only to
|
|
27031
|
+
// ["chatgpt/henry"] to use a named plan, or gateway.order to choose an explicit fallback order.
|
|
27032
|
+
// ChatGPT connections only serve openai/* models; anyone calling/deploying this code uses that plan.
|
|
26858
27033
|
// Model IDs are OpenRouter IDs; GET ${options.url}/v1/models lists them.
|
|
26859
27034
|
export const MODEL = ${JSON.stringify(DEFAULT_MODEL)}
|
|
26860
27035
|
|
|
26861
27036
|
export function services(env: { readonly INFERENCE: ModelBinding }, thread?: string) {
|
|
26862
27037
|
// session lets the platform keep an upstream connection warm for this thread.
|
|
26863
|
-
const model = managedModels(env.INFERENCE, {
|
|
27038
|
+
const model = managedModels(env.INFERENCE, { model: MODEL, ...(thread === undefined ? {} : { session: thread }) })
|
|
26864
27039
|
const tools = weather.implement({
|
|
26865
27040
|
current: ({ city }) => Effect.succeed({ city, temperature: 21 }),
|
|
26866
27041
|
})
|
|
@@ -26987,16 +27162,18 @@ actorctl events main --last 20
|
|
|
26987
27162
|
import { existsSync as existsSync3, readdirSync } from "fs";
|
|
26988
27163
|
import { mkdir as mkdir3, writeFile as writeFile4 } from "fs/promises";
|
|
26989
27164
|
import { dirname as dirname2, join as join5, relative, resolve as resolve5 } from "path";
|
|
27165
|
+
var metadataFlag = optional6("metadata", 'Opaque metadata JSON, e.g. {"tenant":"user_123"}; replaces the map.');
|
|
27166
|
+
var readMetadata = (value) => isSome2(value) ? decodeUnknownEffect2(fromJsonString2(CallMetadata))(value.value).pipe(map7((metadata) => ({ metadata }))) : succeed6({});
|
|
26990
27167
|
var cursor = optional6("cursor", "Continue from a previous page cursor.");
|
|
26991
27168
|
var entry = String10("entry").pipe(optional4, withDescription2("Actor source file; defaults to entry in local config."));
|
|
26992
27169
|
var version = String10("version").pipe(withDescription2("Version identifier."));
|
|
26993
27170
|
var query = (cursor) => isSome2(cursor) ? { cursor: cursor.value } : {};
|
|
26994
|
-
var actorsList = make30("list", { ...remote, cursor }, (flags) => gen2(function* () {
|
|
27171
|
+
var actorsList = make30("list", { ...remote, cursor, metadata: metadataFlag }, (flags) => gen2(function* () {
|
|
26995
27172
|
const config = yield* resolveConfig(flags);
|
|
26996
27173
|
const client = yield* cloudClient(config.url);
|
|
26997
|
-
const result = yield* client.actors.listActors({ params: { account: config.account }, query: query(flags.cursor) });
|
|
27174
|
+
const result = yield* client.actors.listActors({ params: { account: config.account }, query: { ...query(flags.cursor), ...yield* readMetadata(flags.metadata) } });
|
|
26998
27175
|
yield* print(flags.json, result, [
|
|
26999
|
-
...result.items.length ? result.items.map((actor) => `${actor.name} ${actor.description}`) : ["No actors found."],
|
|
27176
|
+
...result.items.length ? result.items.map((actor) => `${actor.name} ${actor.description} ${JSON.stringify(actor.metadata ?? {})}`) : ["No actors found."],
|
|
27000
27177
|
...next(result.cursor)
|
|
27001
27178
|
]);
|
|
27002
27179
|
}).pipe(mapError2(userError))).pipe(withDescription4("List actors in an account."));
|
|
@@ -27007,12 +27184,22 @@ var actorsInfo = make30("info", { ...remote, name: String10("name") }, (flags) =
|
|
|
27007
27184
|
yield* print(flags.json, result, [
|
|
27008
27185
|
result.name,
|
|
27009
27186
|
result.description,
|
|
27187
|
+
`Metadata: ${JSON.stringify(result.metadata ?? {})}`,
|
|
27010
27188
|
`Active deployment: ${result.activeDeployment?.id ?? "none"}`,
|
|
27011
27189
|
`Active version: ${result.activeDeployment?.version ?? "none"}`,
|
|
27012
27190
|
`Endpoint: ${result.endpoint ?? "none"}`,
|
|
27013
27191
|
`Latest deployment: ${result.latestDeployment === null ? "none" : `${result.latestDeployment.id} (${result.latestDeployment.status})`}`
|
|
27014
27192
|
]);
|
|
27015
27193
|
}).pipe(mapError2(userError))).pipe(withDescription4("Inspect an actor and its deployment."));
|
|
27194
|
+
var actorsPut = make30("put", { ...remote, name: String10("name"), description: optional6("description", "Actor description."), metadata: metadataFlag }, (flags) => gen2(function* () {
|
|
27195
|
+
const config = yield* resolveConfig(flags);
|
|
27196
|
+
const client = yield* cloudClient(config.url);
|
|
27197
|
+
const result = yield* client.actors.putActor({ params: { account: config.account, name: flags.name }, payload: {
|
|
27198
|
+
...isSome2(flags.description) ? { description: flags.description.value } : {},
|
|
27199
|
+
...yield* readMetadata(flags.metadata)
|
|
27200
|
+
} });
|
|
27201
|
+
yield* print(flags.json, result, [result.name, result.description, `Metadata: ${JSON.stringify(result.metadata ?? {})}`]);
|
|
27202
|
+
}).pipe(mapError2(userError))).pipe(withDescription4("Register an actor or replace supplied fields; omitted metadata is preserved."));
|
|
27016
27203
|
var versionsList = make30("list", { ...scoped4, cursor }, (flags) => gen2(function* () {
|
|
27017
27204
|
const config = yield* resolveConfig(flags);
|
|
27018
27205
|
const name = yield* requireActor(config);
|
|
@@ -27116,8 +27303,8 @@ var init = make30("init", {
|
|
|
27116
27303
|
" actorctl events main --last 20"
|
|
27117
27304
|
].join(`
|
|
27118
27305
|
`));
|
|
27119
|
-
}).pipe(mapError2(userError))).pipe(withDescription4("Create a tardie agent project
|
|
27120
|
-
var
|
|
27306
|
+
}).pipe(mapError2(userError))).pipe(withDescription4("Create a tardie agent project using managedModels({ model }); options.gateway.only/order selects providers."));
|
|
27307
|
+
var login2 = make30("login", {
|
|
27121
27308
|
url: optional6("url", "Cloud API URL to save with the login.")
|
|
27122
27309
|
}, (flags) => gen2(function* () {
|
|
27123
27310
|
const url = getOrUndefined(flags.url) ?? DEFAULT_CLOUD_URL;
|
|
@@ -27129,23 +27316,24 @@ var login = make30("login", {
|
|
|
27129
27316
|
yield* saveCredentials({ password, ...isSome2(flags.url) ? { url } : {} });
|
|
27130
27317
|
yield* log(`Logged in to ${url} (account ${account}). Saved to ${credentialsDirectory()}/credentials.json.`);
|
|
27131
27318
|
}).pipe(mapError2(userError))).pipe(withDescription4("Save an API key or password to ~/.actorctl."));
|
|
27132
|
-
var
|
|
27319
|
+
var logout2 = make30("logout", {}, () => removeCredentials.pipe(andThen2(log(`Removed ${credentialsDirectory()}/credentials.json.`)), mapError2(userError))).pipe(withDescription4("Remove the saved password."));
|
|
27133
27320
|
var actorctl = make30("actorctl").pipe(withDescription4("Deploy actors, run threads, and read events on Tardigrade Cloud."), withSubcommands([
|
|
27134
27321
|
init,
|
|
27135
|
-
|
|
27136
|
-
|
|
27322
|
+
login2,
|
|
27323
|
+
logout2,
|
|
27324
|
+
chatgpt,
|
|
27137
27325
|
deploy,
|
|
27138
27326
|
threads2,
|
|
27139
27327
|
call,
|
|
27140
27328
|
events2,
|
|
27141
27329
|
sql,
|
|
27142
|
-
make30("actors").pipe(withSubcommands([actorsList, actorsInfo, actorsDelete])),
|
|
27330
|
+
make30("actors").pipe(withSubcommands([actorsList, actorsInfo, actorsPut, actorsDelete])),
|
|
27143
27331
|
make30("versions").pipe(withSubcommands([versionsList, versionsView])),
|
|
27144
27332
|
make30("deployments").pipe(withSubcommands([deploymentsList])),
|
|
27145
27333
|
make30("secrets").pipe(withSubcommands([secretsPut, secretsDelete]))
|
|
27146
27334
|
]));
|
|
27147
27335
|
// package.json
|
|
27148
|
-
var version2 = "0.3.
|
|
27336
|
+
var version2 = "0.3.1-rc.23";
|
|
27149
27337
|
|
|
27150
27338
|
// src/main.ts
|
|
27151
27339
|
run5(actorctl, { version: version2 }).pipe(provide4(mergeAll2(layer10, layer12)), runMain2);
|
package/dist/user-worker.js
CHANGED
|
@@ -17,7 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
18
|
|
|
19
19
|
// ../../src/platform/cloudflare/user-worker.ts
|
|
20
|
-
import { ConfigProvider, Layer as
|
|
20
|
+
import { ConfigProvider, Layer as Layer8 } from "effect";
|
|
21
21
|
|
|
22
22
|
// ../../src/platform/cloudflare/analytics.ts
|
|
23
23
|
import { Effect as Effect2, Layer } from "effect";
|
|
@@ -262,11 +262,11 @@ var run = /* @__PURE__ */ Migrator.make({});
|
|
|
262
262
|
var layer3 = (options) => Layer4.effectDiscard(run(options));
|
|
263
263
|
|
|
264
264
|
// ../../src/platform/cloudflare/runtime.ts
|
|
265
|
-
import { HttpRouter
|
|
265
|
+
import { HttpRouter } from "effect/http";
|
|
266
266
|
import { hostRoutes } from "tardie/platform/http";
|
|
267
267
|
|
|
268
268
|
// ../../src/platform/exporting-host.ts
|
|
269
|
-
import { Context as
|
|
269
|
+
import { Context as Context5, Effect as Effect10, Fiber, Layer as Layer6, ManagedRuntime, Queue, Semaphore as Semaphore3, Schema as Schema2 } from "effect";
|
|
270
270
|
import { SqlClient as SqlClient3 } from "effect/sql";
|
|
271
271
|
import { createThreadHost, InitialState, InvalidMessage as InvalidMessage2, MessageConflict as MessageConflict2 } from "tardie/core";
|
|
272
272
|
|
|
@@ -355,99 +355,29 @@ var sqlEventExporter = (options) => Layer5.unwrap(Effect8.gen(function* () {
|
|
|
355
355
|
return eventExporter({ ...options, source });
|
|
356
356
|
}));
|
|
357
357
|
|
|
358
|
-
// ../../src/platform/actor-mcp.ts
|
|
359
|
-
import { Context as Context5, Effect as Effect9, Layer as Layer6, Schema } from "effect";
|
|
360
|
-
import { McpProtocol, McpSchema, McpServer, Tool } from "effect/ai";
|
|
361
|
-
import { HttpRouter } from "effect/http";
|
|
362
|
-
var ACTOR_MCP_PROTOCOLS = [McpProtocol.v2025_11_25, McpProtocol.v2025_06_18];
|
|
363
|
-
var identity2 = { thread: Schema.NonEmptyString, id: Schema.NonEmptyString };
|
|
364
|
-
var receipt = Schema.Struct({ id: Schema.String, position: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))) });
|
|
365
|
-
function actorMcp(host, instance, options = {}) {
|
|
366
|
-
const path = `/v1/actors/${encodeURIComponent(instance)}/mcp`;
|
|
367
|
-
let app;
|
|
368
|
-
const open = () => app ??= Effect9.runPromise(host.methodContracts({ instance })).then((contracts) => {
|
|
369
|
-
const register = Layer6.effectDiscard(Effect9.gen(function* () {
|
|
370
|
-
const server = yield* McpServer.McpServer;
|
|
371
|
-
const add2 = (name, description, input, output, run2) => server.addTool({
|
|
372
|
-
tool: new McpSchema.Tool({ name, description, inputSchema: Tool.getJsonSchemaFromSchema(input), outputSchema: Tool.getJsonSchemaFromSchema(output) }),
|
|
373
|
-
annotations: Context5.empty(),
|
|
374
|
-
handle: (payload) => Schema.decodeUnknownEffect(Schema.toType(input), { onExcessProperty: "error" })(payload).pipe(
|
|
375
|
-
Effect9.mapError((error) => new McpSchema.InvalidParams({ message: error.message })),
|
|
376
|
-
Effect9.flatMap((value) => run2(value).pipe(
|
|
377
|
-
Effect9.map((result) => new McpSchema.CallToolResult({ content: [{ type: "text", text: JSON.stringify(result) }], structuredContent: result })),
|
|
378
|
-
Effect9.catch((error) => Effect9.succeed(new McpSchema.CallToolResult({ isError: true, content: [{ type: "text", text: error.message }] })))
|
|
379
|
-
))
|
|
380
|
-
)
|
|
381
|
-
});
|
|
382
|
-
const thread = (name) => host.getThread({ instance, thread: name }).pipe(Effect9.flatMap((value) => value ? Effect9.succeed(value) : Effect9.fail(new Error("Unknown thread"))));
|
|
383
|
-
for (const [name, method] of Object.entries(contracts)) {
|
|
384
|
-
yield* add2(
|
|
385
|
-
`invoke_${name}`,
|
|
386
|
-
`Accept ${name}. Returns a durable call receipt; use call_get for the final result. Reuse id and input on retries.`,
|
|
387
|
-
Schema.Struct({ ...identity2, input: method.inputSchema }),
|
|
388
|
-
receipt,
|
|
389
|
-
(input) => thread(input.thread).pipe(Effect9.flatMap((current) => current.invoke(name, input.input, { id: input.id })))
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
yield* add2(
|
|
393
|
-
"call_get",
|
|
394
|
-
"Read a call's current status and final result.",
|
|
395
|
-
Schema.Struct(identity2),
|
|
396
|
-
Schema.Struct({ id: Schema.String, method: Schema.String, status: Schema.String, output: Schema.optionalKey(Schema.Json), error: Schema.optionalKey(Schema.String), reason: Schema.optionalKey(Schema.String) }),
|
|
397
|
-
(input) => thread(input.thread).pipe(Effect9.flatMap((current) => Effect9.gen(function* () {
|
|
398
|
-
const records = yield* current.records();
|
|
399
|
-
const invocation = records.find((record) => record.message?.id === input.id)?.message?.invocation;
|
|
400
|
-
return invocation ? { id: input.id, method: invocation.method, ...yield* current.methodState(invocation.method, input.id) } : void 0;
|
|
401
|
-
})), Effect9.flatMap((value) => value ? Effect9.succeed(value) : Effect9.fail(new Error("Unknown call"))))
|
|
402
|
-
);
|
|
403
|
-
yield* add2(
|
|
404
|
-
"call_cancel",
|
|
405
|
-
"Request cancellation of an accepted call. Returns acceptance, not cancellation completion.",
|
|
406
|
-
Schema.Struct({ ...identity2, reason: Schema.String }),
|
|
407
|
-
receipt,
|
|
408
|
-
(input) => thread(input.thread).pipe(Effect9.flatMap((current) => Effect9.gen(function* () {
|
|
409
|
-
const records = yield* current.records();
|
|
410
|
-
const invocation = records.find((record) => record.message?.id === input.id)?.message?.invocation;
|
|
411
|
-
return invocation ? { id: input.id, method: invocation.method, ...yield* current.methodState(invocation.method, input.id) } : void 0;
|
|
412
|
-
}).pipe(Effect9.flatMap((call) => call ? current.cancel(call.method, input.id, input.reason) : Effect9.fail(new Error("Unknown call"))))))
|
|
413
|
-
);
|
|
414
|
-
})).pipe(Layer6.provide(McpServer.layerHttp({ name: host.actor, version: "1.0.0", path, protocols: ACTOR_MCP_PROTOCOLS, ...options.allowedOrigins ? { allowedOrigins: options.allowedOrigins } : {} })));
|
|
415
|
-
return HttpRouter.toWebHandler(register, { disableLogger: true });
|
|
416
|
-
}).catch((error) => {
|
|
417
|
-
app = void 0;
|
|
418
|
-
throw error;
|
|
419
|
-
});
|
|
420
|
-
return {
|
|
421
|
-
request: async (request) => new URL(request.url).pathname === path ? (await open()).handler(request) : void 0,
|
|
422
|
-
close: async () => {
|
|
423
|
-
if (app) await (await app).dispose();
|
|
424
|
-
}
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
|
|
428
358
|
// src/worker/webhooks.ts
|
|
429
|
-
import { Effect as
|
|
359
|
+
import { Effect as Effect9, Schema } from "effect";
|
|
430
360
|
import { InvalidMessage, MessageConflict } from "tardie/core";
|
|
431
361
|
var REPLY_TO_HEADER = "x-tardigrade-reply-to";
|
|
432
362
|
var WEBHOOK_TRANSPORT = "tardigrade.webhook";
|
|
433
363
|
var REPLY_ACCEPTED = "accepted";
|
|
434
|
-
var CallMetadata =
|
|
435
|
-
|
|
364
|
+
var CallMetadata = Schema.Record(Schema.String, Schema.String.check(Schema.isMaxLength(512))).check(
|
|
365
|
+
Schema.makeFilter((value) => Object.keys(value).length <= 16 && Object.keys(value).every((key) => key.length <= 64) || "Metadata accepts at most 16 pairs with keys up to 64 characters.")
|
|
436
366
|
);
|
|
437
|
-
var ReplyAddress =
|
|
438
|
-
webhook:
|
|
439
|
-
account:
|
|
440
|
-
actor:
|
|
441
|
-
thread:
|
|
442
|
-
method:
|
|
443
|
-
id:
|
|
367
|
+
var ReplyAddress = Schema.Struct({
|
|
368
|
+
webhook: Schema.NonEmptyString,
|
|
369
|
+
account: Schema.NonEmptyString,
|
|
370
|
+
actor: Schema.NonEmptyString,
|
|
371
|
+
thread: Schema.NonEmptyString,
|
|
372
|
+
method: Schema.NonEmptyString,
|
|
373
|
+
id: Schema.NonEmptyString,
|
|
444
374
|
metadata: CallMetadata,
|
|
445
|
-
url:
|
|
375
|
+
url: Schema.String
|
|
446
376
|
});
|
|
447
377
|
var webhookTransport = (binding) => ({
|
|
448
|
-
deliver: (address, body, metadata) =>
|
|
378
|
+
deliver: (address, body, metadata) => Effect9.tryPromise({
|
|
449
379
|
try: async () => {
|
|
450
|
-
const reply =
|
|
380
|
+
const reply = Schema.decodeUnknownSync(ReplyAddress, { onExcessProperty: "error" })(address);
|
|
451
381
|
if (!binding) throw new Error("WEBHOOKS binding is required");
|
|
452
382
|
if (metadata.inReplyTo !== reply.id) throw new Error("Webhook reply identifies a different invocation");
|
|
453
383
|
await binding.enqueueReply(reply, body, metadata.inReplyTo);
|
|
@@ -465,14 +395,14 @@ async function receiveWebhookRequest(request, options) {
|
|
|
465
395
|
const id = request.headers.get("idempotency-key");
|
|
466
396
|
let address;
|
|
467
397
|
try {
|
|
468
|
-
address =
|
|
398
|
+
address = Schema.decodeUnknownSync(ReplyAddress, { onExcessProperty: "error" })(JSON.parse(request.headers.get(REPLY_TO_HEADER)));
|
|
469
399
|
if (!id?.trim() || address.id !== id || address.account !== instance || address.actor !== options.actor || address.thread !== thread || address.method !== method) throw new Error("Reply identity mismatch");
|
|
470
400
|
} catch {
|
|
471
401
|
return Response.json({ code: "invalid_message", message: "Invalid webhook reply address" }, { status: 400 });
|
|
472
402
|
}
|
|
473
403
|
const input = await request.json();
|
|
474
|
-
const
|
|
475
|
-
return Response.json(
|
|
404
|
+
const receipt = await options.receive({ target: { actor: options.actor, instance, thread }, body: { method, input }, message: { id, from: { transport: WEBHOOK_TRANSPORT, address } } });
|
|
405
|
+
return Response.json(receipt, { status: 202, headers: { [REPLY_TO_HEADER]: REPLY_ACCEPTED } });
|
|
476
406
|
}
|
|
477
407
|
const generic = /^\/v1\/actors\/([^/]+)\/threads\/([^/]+)\/calls\/([^/]+)(\/cancel)?$/.exec(path);
|
|
478
408
|
if (generic && (request.method === "GET" && !generic[4] || request.method === "POST" && generic[4])) {
|
|
@@ -482,7 +412,7 @@ async function receiveWebhookRequest(request, options) {
|
|
|
482
412
|
if (request.method === "GET") return Response.json({ id, method: call.method, ...call.state });
|
|
483
413
|
let reason;
|
|
484
414
|
try {
|
|
485
|
-
reason =
|
|
415
|
+
reason = Schema.decodeUnknownSync(Schema.Struct({ reason: Schema.String }), { onExcessProperty: "error" })(await request.json()).reason;
|
|
486
416
|
} catch {
|
|
487
417
|
return Response.json({ code: "invalid_message", message: "A cancellation reason is required" }, { status: 400 });
|
|
488
418
|
}
|
|
@@ -496,7 +426,7 @@ async function receiveWebhookRequest(request, options) {
|
|
|
496
426
|
if (call?.method !== method || !from || !("transport" in from) || from.transport !== WEBHOOK_TRANSPORT) return void 0;
|
|
497
427
|
let reason;
|
|
498
428
|
try {
|
|
499
|
-
reason =
|
|
429
|
+
reason = Schema.decodeUnknownSync(Schema.Struct({ reason: Schema.String }), { onExcessProperty: "error" })(await request.json()).reason;
|
|
500
430
|
} catch {
|
|
501
431
|
return Response.json({ code: "invalid_message", message: "A cancellation reason is required" }, { status: 400 });
|
|
502
432
|
}
|
|
@@ -513,7 +443,7 @@ async function receiveWebhookRequest(request, options) {
|
|
|
513
443
|
}
|
|
514
444
|
|
|
515
445
|
// ../../src/platform/exporting-host.ts
|
|
516
|
-
import { Tool
|
|
446
|
+
import { Tool } from "effect/ai";
|
|
517
447
|
var DEFAULT_EXPORT_FLUSH_TIMEOUT_MS = 3e4;
|
|
518
448
|
var isThreadEventJournal = (value, actor, account) => {
|
|
519
449
|
try {
|
|
@@ -530,16 +460,16 @@ async function createSqlExportingHost(options) {
|
|
|
530
460
|
const runtime = ManagedRuntime.make(options.sql);
|
|
531
461
|
const connections = /* @__PURE__ */ new Set();
|
|
532
462
|
try {
|
|
533
|
-
const state = await runtime.runPromise(
|
|
463
|
+
const state = await runtime.runPromise(Effect10.gen(function* () {
|
|
534
464
|
const sql = yield* SqlClient3.SqlClient;
|
|
535
465
|
yield* sql`CREATE TABLE IF NOT EXISTS event_export_identity (id INTEGER PRIMARY KEY CHECK (id = 1), account TEXT NOT NULL, actor TEXT NOT NULL, generation TEXT NOT NULL)`;
|
|
536
466
|
yield* sql`INSERT INTO event_export_identity VALUES (1, ${options.accountId}, ${options.actor.actorName}, ${crypto.randomUUID()}) ON CONFLICT (id) DO NOTHING`;
|
|
537
467
|
const rows = yield* sql`SELECT account, actor, generation FROM event_export_identity WHERE id = 1`;
|
|
538
|
-
const
|
|
539
|
-
if (
|
|
468
|
+
const identity2 = rows[0];
|
|
469
|
+
if (identity2.account !== options.accountId || identity2.actor !== options.actor.actorName) return yield* Effect10.fail(new Error("Journal database belongs to a different account or actor."));
|
|
540
470
|
const signals = yield* Queue.make({ capacity: 1, strategy: "sliding" });
|
|
541
471
|
const lock = yield* Semaphore3.make(1);
|
|
542
|
-
return { sql, identity:
|
|
472
|
+
return { sql, identity: identity2, signals, lock };
|
|
543
473
|
}));
|
|
544
474
|
let closed = false;
|
|
545
475
|
const signal = () => {
|
|
@@ -549,10 +479,10 @@ async function createSqlExportingHost(options) {
|
|
|
549
479
|
const key = JSON.stringify(parts);
|
|
550
480
|
const opened = options.journalFactory({
|
|
551
481
|
actor: key,
|
|
552
|
-
layer:
|
|
482
|
+
layer: Layer6.succeed(SqlClient3.SqlClient, state.sql),
|
|
553
483
|
...options.transaction ? { transaction: options.transaction } : {},
|
|
554
|
-
flush:
|
|
555
|
-
if (options.sync) yield*
|
|
484
|
+
flush: Effect10.gen(function* () {
|
|
485
|
+
if (options.sync) yield* Effect10.tryPromise({ try: () => options.sync(), catch: (error) => error instanceof Error ? error : new Error(String(error)) });
|
|
556
486
|
if (exportEvents) signal();
|
|
557
487
|
})
|
|
558
488
|
});
|
|
@@ -571,59 +501,57 @@ async function createSqlExportingHost(options) {
|
|
|
571
501
|
checkAccount(coordinate.instance);
|
|
572
502
|
return journal([coordinate.actor, coordinate.instance, "thread", coordinate.thread, "events"], true);
|
|
573
503
|
},
|
|
574
|
-
close:
|
|
504
|
+
close: Effect10.gen(function* () {
|
|
575
505
|
for (const close of connections) yield* close;
|
|
576
506
|
connections.clear();
|
|
577
507
|
})
|
|
578
508
|
};
|
|
579
509
|
const allocations = Semaphore3.makeUnsafe(1);
|
|
580
|
-
const host = createThreadHost({ ...options, services: (coordinate, current) => options.services(coordinate, current).pipe(
|
|
581
|
-
const bootstrap = options.journalFactory({ actor: "bootstrap", layer:
|
|
510
|
+
const host = createThreadHost({ ...options, services: (coordinate, current) => options.services(coordinate, current).pipe(Layer6.provideMerge(Layer6.succeed(SqlClient3.SqlClient, state.sql))), storage, actorContext: options.actorContext ?? ((context) => context) });
|
|
511
|
+
const bootstrap = options.journalFactory({ actor: "bootstrap", layer: Layer6.succeed(SqlClient3.SqlClient, state.sql), ...options.transaction ? { transaction: options.transaction } : {} });
|
|
582
512
|
bootstrap.read();
|
|
583
|
-
await
|
|
584
|
-
const flush = state.lock.withPermits(1)(
|
|
513
|
+
await Effect10.runPromise(bootstrap.close);
|
|
514
|
+
const flush = state.lock.withPermits(1)(Effect10.scoped(Effect10.gen(function* () {
|
|
585
515
|
const rows = yield* state.sql`SELECT DISTINCT actor FROM experimental_events ORDER BY actor`;
|
|
586
516
|
const journals = rows.filter((row) => isThreadEventJournal(row.actor, options.actor.actorName, options.accountId));
|
|
587
517
|
const results = [];
|
|
588
518
|
if (journals.length === 0) return results;
|
|
589
|
-
const analytics =
|
|
519
|
+
const analytics = Context5.get(yield* Layer6.build(options.analytics), Analytics);
|
|
590
520
|
for (const row of journals) {
|
|
591
|
-
const result = yield*
|
|
521
|
+
const result = yield* Effect10.gen(function* () {
|
|
592
522
|
return yield* (yield* EventExporter).flush();
|
|
593
|
-
}).pipe(
|
|
523
|
+
}).pipe(Effect10.provide(sqlEventExporter({
|
|
594
524
|
accountId: options.accountId,
|
|
595
525
|
actor: options.actor.actorName,
|
|
596
526
|
sourceId: JSON.stringify([state.identity.generation, row.actor]),
|
|
597
527
|
destinationId: options.destinationId,
|
|
598
528
|
journal: row.actor,
|
|
599
529
|
...options.batchSize === void 0 ? {} : { batchSize: options.batchSize }
|
|
600
|
-
}).pipe(
|
|
530
|
+
}).pipe(Layer6.provide(Layer6.succeed(Analytics, analytics)))));
|
|
601
531
|
results.push(result);
|
|
602
532
|
}
|
|
603
533
|
return results;
|
|
604
|
-
})).pipe(
|
|
605
|
-
const worker = runtime.runFork(
|
|
534
|
+
})).pipe(Effect10.timeout(flushTimeoutMs)));
|
|
535
|
+
const worker = runtime.runFork(Effect10.forever(Effect10.gen(function* () {
|
|
606
536
|
yield* Queue.take(state.signals);
|
|
607
537
|
yield* flush.pipe(
|
|
608
|
-
|
|
538
|
+
Effect10.tap((results) => Effect10.sync(() => {
|
|
609
539
|
if (results.some((result) => result.hasMore)) signal();
|
|
610
540
|
})),
|
|
611
|
-
|
|
541
|
+
Effect10.catch((error) => Effect10.logWarning("Actor analytics export failed; the cursor is retained for the next flush.", { operation: error instanceof ExportError ? error.operation : "connection", flushTimeoutMs }))
|
|
612
542
|
);
|
|
613
543
|
})));
|
|
614
544
|
signal();
|
|
615
|
-
const mcp = actorMcp(host, options.accountId, options.mcp);
|
|
616
545
|
let closing;
|
|
617
546
|
return {
|
|
618
547
|
...host,
|
|
619
|
-
mcpRequest: mcp.request,
|
|
620
548
|
allocationRequest: async (request) => {
|
|
621
549
|
const parts = new URL(request.url).pathname.split("/");
|
|
622
550
|
if (request.method !== "POST" || parts.length !== 5 || parts[1] !== "v1" || parts[2] !== "actors" || parts[4] !== "threads") return void 0;
|
|
623
551
|
const input = await request.json().catch(() => void 0);
|
|
624
|
-
const schema =
|
|
625
|
-
const work = allocations.withPermit(
|
|
626
|
-
const body = yield*
|
|
552
|
+
const schema = Schema2.Struct({ name: Schema2.optionalKey(Schema2.NonEmptyString), parent: Schema2.optionalKey(Schema2.NonEmptyString), initialState: Schema2.optionalKey(InitialState) });
|
|
553
|
+
const work = allocations.withPermit(Effect10.gen(function* () {
|
|
554
|
+
const body = yield* Schema2.decodeUnknownEffect(schema)(input).pipe(Effect10.mapError(InvalidMessage2.from));
|
|
627
555
|
const instance = decodeURIComponent(parts[3]);
|
|
628
556
|
if (body.name && (yield* host.getThread({ instance, thread: body.name }))) return Response.json({ code: "thread_exists", message: "Thread already exists." }, { status: 409 });
|
|
629
557
|
const name = body.name === void 0 ? {} : { name: body.name };
|
|
@@ -631,22 +559,22 @@ async function createSqlExportingHost(options) {
|
|
|
631
559
|
const thread = yield* body.parent === void 0 ? host.allocateRootThread({ instance, ...name, ...seed }) : host.allocateChildThread({ parent: { actor: host.actor, instance, thread: body.parent }, ...name, ...seed });
|
|
632
560
|
return Response.json(thread.coordinate);
|
|
633
561
|
}));
|
|
634
|
-
return
|
|
562
|
+
return Effect10.runPromise(work.pipe(Effect10.catch((error) => Effect10.succeed(Response.json({ error: error.message }, { status: error instanceof InvalidMessage2 ? 400 : 500 })))));
|
|
635
563
|
},
|
|
636
564
|
methodRequest: async (request) => {
|
|
637
565
|
const parts = new URL(request.url).pathname.split("/");
|
|
638
566
|
if (parts[1] !== "v1" || parts[2] !== "actors" || decodeURIComponent(parts[3] ?? "") !== options.accountId) return void 0;
|
|
639
567
|
if (request.method === "GET" && parts.length === 5 && parts[4] === "methods") {
|
|
640
|
-
const contracts = await
|
|
568
|
+
const contracts = await Effect10.runPromise(host.methodContracts({ instance: options.accountId }));
|
|
641
569
|
return Response.json({ items: Object.entries(contracts).map(([name, method]) => ({
|
|
642
570
|
name,
|
|
643
|
-
inputSchema:
|
|
644
|
-
outputSchema:
|
|
571
|
+
inputSchema: Tool.getJsonSchemaFromSchema(method.inputSchema),
|
|
572
|
+
outputSchema: Tool.getJsonSchemaFromSchema(method.outputSchema),
|
|
645
573
|
cancellable: method.onCancel !== void 0
|
|
646
574
|
})) });
|
|
647
575
|
}
|
|
648
576
|
if (parts[4] === "threads" && parts[6] === "calls" && (request.method === "GET" && parts.length === 8 || request.method === "POST" && parts.length === 9 && parts[8] === "cancel")) {
|
|
649
|
-
const work2 =
|
|
577
|
+
const work2 = Effect10.gen(function* () {
|
|
650
578
|
const thread = yield* host.getThread({ instance: options.accountId, thread: decodeURIComponent(parts[5]) });
|
|
651
579
|
if (!thread) return new Response(null, { status: 404 });
|
|
652
580
|
const id2 = decodeURIComponent(parts[7]);
|
|
@@ -655,26 +583,26 @@ async function createSqlExportingHost(options) {
|
|
|
655
583
|
const call = invocation ? { id: id2, method: invocation.method, ...yield* thread.methodState(invocation.method, id2) } : void 0;
|
|
656
584
|
if (!call) return new Response(null, { status: 404 });
|
|
657
585
|
if (request.method === "GET") return Response.json(call);
|
|
658
|
-
const input2 = yield*
|
|
659
|
-
const body = yield*
|
|
586
|
+
const input2 = yield* Effect10.tryPromise(() => request.json()).pipe(Effect10.mapError(InvalidMessage2.from));
|
|
587
|
+
const body = yield* Schema2.decodeUnknownEffect(Schema2.Struct({ reason: Schema2.String }), { onExcessProperty: "error" })(input2).pipe(Effect10.mapError(InvalidMessage2.from));
|
|
660
588
|
const original = records.find((record) => record.message?.id === id2)?.message;
|
|
661
|
-
const
|
|
662
|
-
return Response.json(
|
|
589
|
+
const receipt = original?.from && "transport" in original.from && original.from.transport === WEBHOOK_TRANSPORT ? yield* host.receive({ target: thread.coordinate, body: { method: call.method, cancel: { id: id2, reason: body.reason } }, message: { id: JSON.stringify(["cancel", call.method, id2]), from: original.from } }) : yield* thread.cancel(call.method, id2, body.reason);
|
|
590
|
+
return Response.json(receipt, { status: 202 });
|
|
663
591
|
});
|
|
664
|
-
return
|
|
592
|
+
return Effect10.runPromise(work2.pipe(Effect10.catch((error) => Effect10.succeed(Response.json({ error: error.message }, { status: error instanceof InvalidMessage2 ? 400 : error instanceof MessageConflict2 ? 409 : 500 })))));
|
|
665
593
|
}
|
|
666
594
|
if (request.method === "POST" && parts.length === 8 && parts[4] === "threads" && parts[6] === "methods" && request.headers.has("x-tardigrade-reply-to")) {
|
|
667
|
-
const thread = await
|
|
595
|
+
const thread = await Effect10.runPromise(host.getThread({ instance: options.accountId, thread: decodeURIComponent(parts[5]) }));
|
|
668
596
|
if (!thread || !Object.hasOwn(thread.methods, decodeURIComponent(parts[7]))) return new Response(null, { status: 404 });
|
|
669
597
|
}
|
|
670
598
|
const webhook = await receiveWebhookRequest(request.clone(), {
|
|
671
599
|
actor: host.actor,
|
|
672
|
-
receive: (packet) =>
|
|
600
|
+
receive: (packet) => Effect10.runPromise(host.receive(packet)),
|
|
673
601
|
lookup: async (instance, thread, id2) => {
|
|
674
|
-
const handle = await
|
|
602
|
+
const handle = await Effect10.runPromise(host.getThread({ instance, thread }));
|
|
675
603
|
if (!handle) return void 0;
|
|
676
|
-
const message = (await
|
|
677
|
-
return message?.invocation ? { method: message.invocation.method, from: message.from, state: await
|
|
604
|
+
const message = (await Effect10.runPromise(handle.records())).find((record) => record.message?.id === id2 && record.message.invocation)?.message;
|
|
605
|
+
return message?.invocation ? { method: message.invocation.method, from: message.from, state: await Effect10.runPromise(handle.methodState(message.invocation.method, id2)) } : void 0;
|
|
678
606
|
}
|
|
679
607
|
}).catch((error) => Response.json({ error: error.message }, { status: error instanceof MessageConflict2 ? 409 : error instanceof InvalidMessage2 || error instanceof SyntaxError ? 400 : 500 }));
|
|
680
608
|
if (webhook) return webhook;
|
|
@@ -682,15 +610,15 @@ async function createSqlExportingHost(options) {
|
|
|
682
610
|
const id = request.headers.get("idempotency-key");
|
|
683
611
|
if (!id?.trim()) return Response.json({ error: "Idempotency-Key is required" }, { status: 400 });
|
|
684
612
|
const input = await request.json().catch(() => void 0);
|
|
685
|
-
const work =
|
|
613
|
+
const work = Effect10.gen(function* () {
|
|
686
614
|
const thread = yield* host.getThread({ instance: decodeURIComponent(parts[3]), thread: decodeURIComponent(parts[5]) });
|
|
687
615
|
const method = decodeURIComponent(parts[7]);
|
|
688
616
|
if (!thread || !Object.hasOwn(thread.methods, method)) return new Response(null, { status: 404 });
|
|
689
|
-
const body = yield*
|
|
690
|
-
const
|
|
691
|
-
return Response.json(
|
|
617
|
+
const body = yield* Schema2.decodeUnknownEffect(Schema2.Json)(input).pipe(Effect10.mapError(InvalidMessage2.from));
|
|
618
|
+
const receipt = yield* thread.invoke(method, body, { id });
|
|
619
|
+
return Response.json(receipt, { status: 202 });
|
|
692
620
|
});
|
|
693
|
-
return
|
|
621
|
+
return Effect10.runPromise(work.pipe(Effect10.catch((error) => Effect10.succeed(Response.json({ error: error.message }, { status: error instanceof MessageConflict2 ? 409 : error instanceof InvalidMessage2 ? 400 : 500 })))));
|
|
694
622
|
},
|
|
695
623
|
flushEvents: () => {
|
|
696
624
|
if (closed) return Promise.reject(new Error("Actor host is closed."));
|
|
@@ -699,19 +627,15 @@ async function createSqlExportingHost(options) {
|
|
|
699
627
|
close: () => closing ??= (async () => {
|
|
700
628
|
closed = true;
|
|
701
629
|
try {
|
|
702
|
-
|
|
703
|
-
await mcp.close();
|
|
704
|
-
} finally {
|
|
705
|
-
await Effect11.runPromise(host.close);
|
|
706
|
-
}
|
|
630
|
+
await Effect10.runPromise(host.close);
|
|
707
631
|
} finally {
|
|
708
|
-
await
|
|
632
|
+
await Effect10.runPromise(Fiber.interrupt(worker));
|
|
709
633
|
await runtime.dispose();
|
|
710
634
|
}
|
|
711
635
|
})()
|
|
712
636
|
};
|
|
713
637
|
} catch (error) {
|
|
714
|
-
for (const close of connections) await
|
|
638
|
+
for (const close of connections) await Effect10.runPromise(close);
|
|
715
639
|
await runtime.dispose();
|
|
716
640
|
throw error;
|
|
717
641
|
}
|
|
@@ -755,27 +679,63 @@ var eventsPage = (rows, query) => ({
|
|
|
755
679
|
});
|
|
756
680
|
|
|
757
681
|
// ../../src/platform/cloudflare/router.ts
|
|
758
|
-
import { Effect as Effect16, Layer as
|
|
682
|
+
import { Effect as Effect16, Layer as Layer7 } from "effect";
|
|
759
683
|
|
|
760
684
|
// ../../src/services/actor-router.ts
|
|
761
|
-
import { Context as
|
|
685
|
+
import { Context as Context6, Data as Data4 } from "effect";
|
|
762
686
|
var ActorRouterError = class extends Data4.TaggedError("ActorRouterError") {
|
|
763
687
|
};
|
|
764
|
-
var ActorRouter = class extends
|
|
688
|
+
var ActorRouter = class extends Context6.Service()("tardigrade-cloud/ActorRouter") {
|
|
765
689
|
};
|
|
766
690
|
|
|
767
691
|
// ../../src/services/actor-runtime.ts
|
|
768
|
-
import { Context as
|
|
692
|
+
import { Context as Context7, Data as Data5 } from "effect";
|
|
769
693
|
var ActorRuntimeError = class extends Data5.TaggedError("ActorRuntimeError") {
|
|
770
694
|
};
|
|
771
|
-
var ActorRuntime = class extends
|
|
695
|
+
var ActorRuntime = class extends Context7.Service()("tardigrade-cloud/ActorRuntime") {
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
// ../../src/services/metadata-store.ts
|
|
699
|
+
import { Context as Context9, Data as Data7, Schema as Schema4 } from "effect";
|
|
700
|
+
|
|
701
|
+
// ../../src/services/webhooks.ts
|
|
702
|
+
import { Context as Context8, Data as Data6, Schema as Schema3 } from "effect";
|
|
703
|
+
var WebhookEventType = Schema3.Literals(["call.completed", "call.failed", "call.cancelled", "webhook.test"]);
|
|
704
|
+
var WebhookEndpoint = Schema3.Struct({
|
|
705
|
+
id: Schema3.String,
|
|
706
|
+
url: Schema3.String,
|
|
707
|
+
events: Schema3.Array(WebhookEventType),
|
|
708
|
+
description: Schema3.String,
|
|
709
|
+
status: Schema3.Literals(["active", "disabled"]),
|
|
710
|
+
createdAt: Schema3.String
|
|
711
|
+
});
|
|
712
|
+
var WebhookCreate = Schema3.Struct({ url: Schema3.String, events: Schema3.optionalKey(Schema3.Array(WebhookEventType)), description: Schema3.optionalKey(Schema3.String) });
|
|
713
|
+
var WebhookPatch = Schema3.Struct({
|
|
714
|
+
url: Schema3.optionalKey(Schema3.String),
|
|
715
|
+
events: Schema3.optionalKey(Schema3.Array(WebhookEventType)),
|
|
716
|
+
description: Schema3.optionalKey(Schema3.String),
|
|
717
|
+
status: Schema3.optionalKey(Schema3.Literals(["active", "disabled"]))
|
|
718
|
+
});
|
|
719
|
+
var WebhookError = class extends Data6.TaggedError("WebhookError") {
|
|
720
|
+
};
|
|
721
|
+
var WebhookStore = class extends Context8.Service()("tardigrade-cloud/WebhookStore") {
|
|
722
|
+
};
|
|
723
|
+
var WebhookAttempt = Schema3.Struct({ startedAt: Schema3.Number, finishedAt: Schema3.NullOr(Schema3.Number), statusCode: Schema3.NullOr(Schema3.Number), error: Schema3.NullOr(Schema3.String) });
|
|
724
|
+
var WebhookDelivery = Schema3.Struct({
|
|
725
|
+
id: Schema3.String,
|
|
726
|
+
type: WebhookEventType,
|
|
727
|
+
status: Schema3.Literals(["pending", "delivered", "failed", "skipped"]),
|
|
728
|
+
createdAt: Schema3.Number,
|
|
729
|
+
attempts: Schema3.Array(WebhookAttempt)
|
|
730
|
+
});
|
|
731
|
+
var WebhookDeliveryService = class extends Context8.Service()("tardigrade-cloud/WebhookDeliveryService") {
|
|
772
732
|
};
|
|
773
733
|
|
|
774
734
|
// ../../src/services/metadata-store.ts
|
|
775
|
-
import { Context as Context9, Data as Data6, Schema as Schema4 } from "effect";
|
|
776
735
|
var Actor = Schema4.Struct({
|
|
777
736
|
name: Schema4.NonEmptyString,
|
|
778
|
-
description: Schema4.String
|
|
737
|
+
description: Schema4.String,
|
|
738
|
+
metadata: Schema4.optionalKey(CallMetadata)
|
|
779
739
|
}).annotate({ identifier: "Actor" });
|
|
780
740
|
var Version = Schema4.Struct({
|
|
781
741
|
id: Schema4.NonEmptyString,
|
|
@@ -790,7 +750,7 @@ var VersionRecord = Schema4.Struct({
|
|
|
790
750
|
...Version.fields,
|
|
791
751
|
artifactDigest: Schema4.String.check(Schema4.isPattern(/^sha256:[a-f0-9]{64}$/))
|
|
792
752
|
}).annotate({ identifier: "VersionRecord" });
|
|
793
|
-
var MetadataStoreError = class extends
|
|
753
|
+
var MetadataStoreError = class extends Data7.TaggedError("MetadataStoreError") {
|
|
794
754
|
};
|
|
795
755
|
var MetadataStore = class extends Context9.Service()("tardigrade-cloud/MetadataStore") {
|
|
796
756
|
};
|
|
@@ -803,7 +763,7 @@ var actorObjectName = (address) => JSON.stringify([address.accountId, address.ac
|
|
|
803
763
|
|
|
804
764
|
// ../../src/platform/cloudflare/runtime.ts
|
|
805
765
|
var createCloudflareExportingHost = (options) => import("tardie/platform/journal").then(({ sqlJournal }) => createSqlExportingHost({ ...options, journalFactory: sqlJournal, sql: SqliteClient_exports.layer({ storage: options.storage }), transaction: (work) => options.storage.transactionSync(work), sync: () => options.storage.sync() }));
|
|
806
|
-
var cloudflareHandler = (host) =>
|
|
766
|
+
var cloudflareHandler = (host) => HttpRouter.toWebHandler(hostRoutes(host), { disableLogger: true });
|
|
807
767
|
var DEFAULT_EVENT_PAGE_SIZE = 100;
|
|
808
768
|
var READY_PATH = "/__tardigrade/ready";
|
|
809
769
|
var cloudflareActorObject = (options) => class {
|
|
@@ -876,7 +836,7 @@ var cloudflareActorObject = (options) => class {
|
|
|
876
836
|
const input = await request.clone().json().catch(() => null);
|
|
877
837
|
if (typeof input?.name === "string" && await Effect17.runPromise(this.active.host.getThread({ instance: accountId, thread: input.name }))) return Response.json({ code: "thread_exists", message: "Thread already exists." }, { status: 409 });
|
|
878
838
|
}
|
|
879
|
-
return await this.active.host.allocationRequest(request.clone()) ?? await this.active.host.methodRequest(request.clone()) ??
|
|
839
|
+
return await this.active.host.allocationRequest(request.clone()) ?? await this.active.host.methodRequest(request.clone()) ?? this.active.app.handler(request);
|
|
880
840
|
}
|
|
881
841
|
};
|
|
882
842
|
|
|
@@ -896,11 +856,11 @@ var createActorWorker = (actor, DurableObject, services, actorContext) => {
|
|
|
896
856
|
transports: { [WEBHOOK_TRANSPORT]: webhookTransport(env.WEBHOOKS) },
|
|
897
857
|
...actorContext ? { actorContext } : {},
|
|
898
858
|
services: (coordinate, runtime) => {
|
|
899
|
-
const platform =
|
|
859
|
+
const platform = Layer8.mergeAll(
|
|
900
860
|
ConfigProvider.layer(ConfigProvider.fromUnknown(secrets(env.SECRETS))),
|
|
901
861
|
boundAiGateway(deployment.accountId, env.INFERENCE)
|
|
902
862
|
);
|
|
903
|
-
return services ? services(runtime, coordinate).pipe(
|
|
863
|
+
return services ? services(runtime, coordinate).pipe(Layer8.provideMerge(platform)) : platform;
|
|
904
864
|
},
|
|
905
865
|
analytics: cloudflareAnalytics(deployment.accountId, env.ANALYTICS),
|
|
906
866
|
destinationId: "aurora",
|
package/dist/worker/models.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Managed models for actors deployed to Tardigrade Cloud. An actor names a catalog model, e.g.
|
|
2
|
-
// "anthropic/claude-sonnet-5.5"
|
|
2
|
+
// "anthropic/claude-sonnet-5.5". API pricing (credits) is the default. options.gateway.only/order
|
|
3
|
+
// select catalog hosts or workspace connections such as "chatgpt/henry" for openai/* models.
|
|
3
4
|
// The actor always sends its full prompt; the binding may send less upstream, but never needs the actor's help.
|
|
4
5
|
import { Effect, Layer, Schema, Stream } from "effect"
|
|
5
6
|
import { AiError, LanguageModel, Prompt, Response, Tool } from "effect/ai"
|
|
@@ -29,8 +30,9 @@ export type ResponseFormatSpec =
|
|
|
29
30
|
| { readonly type: "text" }
|
|
30
31
|
| { readonly type: "json"; readonly objectName: string; readonly schema: JsonObject }
|
|
31
32
|
|
|
32
|
-
// ModelOptions
|
|
33
|
+
// ModelOptions configure generation and how the request is routed.
|
|
33
34
|
export interface ModelOptions {
|
|
35
|
+
readonly gateway?: { readonly order?: ReadonlyArray<string>; readonly only?: ReadonlyArray<string> }
|
|
34
36
|
readonly maxOutputTokens?: number
|
|
35
37
|
readonly temperature?: number
|
|
36
38
|
readonly serviceTier?: "auto" | "default" | "flex" | "priority"
|
|
@@ -51,7 +53,9 @@ export interface ModelRequest {
|
|
|
51
53
|
readonly session?: string
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
const errorJsonCodec = Schema.toCodecJson(AiError.AiError)
|
|
57
|
+
const durationJsonCodec = Schema.toCodecJson(Schema.Duration)
|
|
58
|
+
export type EncodedAiError = typeof errorJsonCodec.Encoded
|
|
55
59
|
export type GenerateResult = { readonly parts: ReadonlyArray<Response.PartEncoded> } | { readonly error: EncodedAiError }
|
|
56
60
|
// The stream body is newline-delimited JSON, one StreamFrame per line. An error frame ends the stream.
|
|
57
61
|
export type StreamFrame = { readonly part: Response.StreamPartEncoded } | { readonly error: EncodedAiError }
|
|
@@ -89,8 +93,15 @@ export interface ModelBinding {
|
|
|
89
93
|
readonly stream: (request: ModelRequest) => Promise<ReadableStream<Uint8Array>>
|
|
90
94
|
}
|
|
91
95
|
|
|
92
|
-
export const encodeAiError = (error: AiError.AiError): EncodedAiError =>
|
|
93
|
-
|
|
96
|
+
export const encodeAiError = (error: AiError.AiError): EncodedAiError => {
|
|
97
|
+
const encoded = Schema.encodeSync(AiError.AiError)(error)
|
|
98
|
+
const reason = encoded.reason._tag === "RateLimitError" && encoded.reason.retryAfter !== undefined
|
|
99
|
+
? { ...encoded.reason, retryAfter: Schema.encodeSync(durationJsonCodec)(encoded.reason.retryAfter) }
|
|
100
|
+
: encoded.reason
|
|
101
|
+
// Basic error encoding retains Redacted markers; JSON serialization sanitizes them before transport.
|
|
102
|
+
return JSON.parse(JSON.stringify({ ...encoded, reason })) as EncodedAiError
|
|
103
|
+
}
|
|
104
|
+
export const decodeAiError = (error: EncodedAiError): AiError.AiError => Schema.decodeSync(errorJsonCodec)(error)
|
|
94
105
|
|
|
95
106
|
// --- Actor side ---
|
|
96
107
|
|
|
@@ -101,8 +112,29 @@ const failure = (method: string, description: string) => AiError.make({
|
|
|
101
112
|
|
|
102
113
|
const invalid = (method: string, description: string) => AiError.make({ module: "TardigradeModels", method, reason: new AiError.InvalidRequestError({ description }) })
|
|
103
114
|
|
|
115
|
+
// Actor-side validation uses the model families it knows; the engine also checks exact catalog routes.
|
|
116
|
+
export const validateGateway = (model: string, gateway: ModelOptions["gateway"]): string | undefined => {
|
|
117
|
+
if (gateway === undefined) return undefined
|
|
118
|
+
if (!gateway || typeof gateway !== "object" || Array.isArray(gateway)) return "gateway must contain order and/or only arrays."
|
|
119
|
+
const hosts = ["openai", "azure", "bedrock", "anthropic", "baseten", "cheaperinference", "google"]
|
|
120
|
+
const family = model.startsWith("openai/") ? ["openai", "azure"] : model.startsWith("anthropic/") ? ["bedrock", "anthropic"] : model.startsWith("google/") ? ["cheaperinference", "google"] : undefined
|
|
121
|
+
for (const list of [gateway.order, gateway.only]) {
|
|
122
|
+
if (list === undefined) continue
|
|
123
|
+
if (!Array.isArray(list)) return "gateway.order and gateway.only must be arrays of provider slugs."
|
|
124
|
+
for (const slug of list) {
|
|
125
|
+
if (typeof slug !== "string") return "Gateway provider slugs must be strings."
|
|
126
|
+
if (/^chatgpt\/[a-z0-9-]{1,64}$/.test(slug)) {
|
|
127
|
+
if (!model.startsWith("openai/")) return `Gateway slug '${slug}' cannot serve ${model}; ChatGPT connections require openai/* models.`
|
|
128
|
+
} else if (!hosts.includes(slug)) return `Unknown gateway slug '${slug}'. Use a catalog host or chatgpt/<name>.`
|
|
129
|
+
else if (family && !family.includes(slug)) return `Gateway slug '${slug}' does not serve ${model}.`
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
104
134
|
// requestOf turns effect/ai provider options into the wire request. Tools travel as JSON Schema; the host rebuilds them.
|
|
105
135
|
export const requestOf = (provider: LanguageModel.ProviderOptions, model: string, options: ModelOptions | undefined, session: string | undefined, method: string) => Effect.gen(function* () {
|
|
136
|
+
const routingError = validateGateway(model, options?.gateway)
|
|
137
|
+
if (routingError) return yield* invalid(method, routingError)
|
|
106
138
|
const tools: ToolSpec[] = []
|
|
107
139
|
for (const tool of provider.tools) {
|
|
108
140
|
if (Tool.isProviderDefined(tool)) return yield* invalid(method, `Provider-defined tool ${tool.name} is not served by managed models.`)
|
|
@@ -144,9 +176,11 @@ export const managedLanguageModel = (binding: ModelBinding, model: string, optio
|
|
|
144
176
|
})
|
|
145
177
|
|
|
146
178
|
export interface ManagedModelsOptions {
|
|
147
|
-
//
|
|
148
|
-
readonly
|
|
149
|
-
|
|
179
|
+
// model is the main catalog model; see GET /v1/models.
|
|
180
|
+
readonly model?: string
|
|
181
|
+
/** @deprecated Use model. Providing both is an error. */
|
|
182
|
+
readonly default?: string
|
|
183
|
+
// allow lists the other catalog models the actor may select. The main model is always allowed.
|
|
150
184
|
readonly allow?: ReadonlyArray<string>
|
|
151
185
|
// options apply to every model, or per model ID.
|
|
152
186
|
readonly options?: ModelOptions | Readonly<Record<string, ModelOptions>>
|
|
@@ -195,19 +229,24 @@ const catalogOf = (binding: ModelBinding) => {
|
|
|
195
229
|
|
|
196
230
|
// managedModels supplies tardie's model services from the platform catalog, read through the binding.
|
|
197
231
|
export const managedModels = (binding: ModelBinding, settings: ManagedModelsOptions) => Layer.unwrap(Effect.gen(function* () {
|
|
198
|
-
|
|
232
|
+
if (settings.model !== undefined && settings.default !== undefined) return yield* Effect.fail(new Error("Use model or deprecated default, not both."))
|
|
233
|
+
const main = settings.model ?? settings.default
|
|
234
|
+
if (!main) return yield* Effect.fail(new Error("managedModels requires model (or deprecated default)."))
|
|
235
|
+
const wanted = [...new Set([main, ...(settings.allow ?? [])])]
|
|
199
236
|
const listed = yield* Effect.tryPromise({ try: () => catalogOf(binding), catch: error => new Error(`Managed model catalog is unavailable: ${String(error)}`) })
|
|
200
237
|
const models = wanted.map(id => listed.data.find(model => model.id === id))
|
|
201
238
|
const missing = wanted.filter((_, index) => models[index] === undefined)
|
|
202
239
|
if (missing.length > 0) return yield* Effect.fail(new Error(`Not managed models: ${missing.join(", ")}. See GET /v1/models.`))
|
|
203
240
|
const catalog = models as ReadonlyArray<ListedModel>
|
|
204
|
-
const config = modelConfigOf({ allow: "*", providers: { [MANAGED_PROVIDER]: CONNECTION }, default: { provider: MANAGED_PROVIDER, model_id:
|
|
241
|
+
const config = modelConfigOf({ allow: "*", providers: { [MANAGED_PROVIDER]: CONNECTION }, default: { provider: MANAGED_PROVIDER, model_id: main } })
|
|
205
242
|
const lock = layerModelLock(lockOf(catalog), config)
|
|
206
243
|
const structured = new Set(catalog.filter(model => model.structuredOutput).map(model => model.id))
|
|
207
244
|
return liveModelServices({
|
|
208
245
|
credentials: { [CREDENTIAL]: "managed" },
|
|
209
246
|
...(settings.timeoutMs === undefined ? {} : { timeoutMs: settings.timeoutMs }),
|
|
210
|
-
configure: selected => ({
|
|
247
|
+
configure: selected => ({
|
|
248
|
+
...(settings.timeoutMs == null ? {} : { timeout: { firstChunkMs: settings.timeoutMs, idleMs: settings.timeoutMs } }),
|
|
249
|
+
output: structured.has(selected.model_id) ? { guarantee: "native", withTools: true } : { guarantee: "none" } }),
|
|
211
250
|
providerLayer: provider => {
|
|
212
251
|
// tardie's policy lands in the provider config; carry its output ceiling unless the actor set one.
|
|
213
252
|
const ceiling = (provider.model.config as { max_output_tokens?: number } | undefined)?.max_output_tokens
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "actorctl",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1-rc.23",
|
|
4
4
|
"description": "Deploy actors, run threads, and read events on Tardigrade Cloud.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -26,6 +26,6 @@
|
|
|
26
26
|
"tardie": "*"
|
|
27
27
|
},
|
|
28
28
|
"tardigrade": {
|
|
29
|
-
"sourceTree": "
|
|
29
|
+
"sourceTree": "289c3fd4bb0754c9a39428c8992358697067e8f8"
|
|
30
30
|
}
|
|
31
31
|
}
|