@hitch42/applet 0.1.1-beta.1
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/LICENSE +21 -0
- package/dist/_internal/client.d.ts +7 -0
- package/dist/_internal/client.js +60 -0
- package/dist/_internal/server.d.ts +79 -0
- package/dist/_internal/server.js +232 -0
- package/dist/applet.d.ts +1 -0
- package/dist/applet.js +160 -0
- package/dist/auth-DaVK0k5R.js +350 -0
- package/dist/batch-Qa4B4-Yp.d.ts +30 -0
- package/dist/build-CEku1lyb.js +2 -0
- package/dist/build-Rk3xDZV7.js +74 -0
- package/dist/client.d.ts +48 -0
- package/dist/client.js +38 -0
- package/dist/contract-DMrpbaLJ.d.ts +23 -0
- package/dist/deploy-D3BGDqgC.js +2416 -0
- package/dist/generate-6Nbt7uii.js +2 -0
- package/dist/generate-CH0VQmUi.js +484 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +136 -0
- package/dist/init-BS1Y-eNL.js +240 -0
- package/dist/lazy-client-x0DCI98S.js +64 -0
- package/dist/load-config-BnxocLAQ.js +56 -0
- package/dist/logs-m00pzr_O.js +183 -0
- package/dist/manifest-BQCAxATS.d.ts +81 -0
- package/dist/manifest-DyoJ1MIL.js +174 -0
- package/dist/project-CxN-Dtc7.js +346 -0
- package/dist/react.d.ts +37 -0
- package/dist/react.js +517 -0
- package/dist/server.d.ts +46 -0
- package/dist/server.js +104 -0
- package/dist/upload-DRbVhuDm.js +114 -0
- package/dist/upload-YbRKEqR6.d.ts +112 -0
- package/dist/variables-CP7Ce3Np.js +133 -0
- package/dist/vite.d.ts +63 -0
- package/dist/vite.js +358 -0
- package/dist/workflow-CTo2kZF1.js +51 -0
- package/dist/workflow-DurM6Fwx.d.ts +28 -0
- package/package.json +104 -0
- package/scaffold/.agents/skills/hitch-applets/SKILL.md +137 -0
- package/scaffold/.agents/skills/hitch-applets/references/client.md +73 -0
- package/scaffold/.agents/skills/hitch-applets/references/files.md +109 -0
- package/scaffold/.agents/skills/hitch-applets/references/manifest.md +171 -0
- package/scaffold/.agents/skills/hitch-applets/references/markdown.md +37 -0
- package/scaffold/.agents/skills/hitch-applets/references/records.md +152 -0
- package/scaffold/.agents/skills/hitch-applets/references/schedules.md +86 -0
- package/scaffold/.agents/skills/hitch-applets/references/server.md +98 -0
- package/scaffold/.agents/skills/hitch-applets/references/workflows.md +73 -0
- package/scaffold/AGENTS.md +38 -0
- package/scaffold/applet/components.json +25 -0
- package/scaffold/applet/src/client/components/ui/alert.tsx +73 -0
- package/scaffold/applet/src/client/components/ui/avatar.tsx +100 -0
- package/scaffold/applet/src/client/components/ui/badge.tsx +55 -0
- package/scaffold/applet/src/client/components/ui/button.tsx +90 -0
- package/scaffold/applet/src/client/components/ui/card.tsx +85 -0
- package/scaffold/applet/src/client/components/ui/checkbox.tsx +35 -0
- package/scaffold/applet/src/client/components/ui/dialog.tsx +164 -0
- package/scaffold/applet/src/client/components/ui/dropdown-menu.tsx +235 -0
- package/scaffold/applet/src/client/components/ui/input-group.tsx +144 -0
- package/scaffold/applet/src/client/components/ui/input.tsx +22 -0
- package/scaffold/applet/src/client/components/ui/label.tsx +29 -0
- package/scaffold/applet/src/client/components/ui/popover.tsx +74 -0
- package/scaffold/applet/src/client/components/ui/select.tsx +243 -0
- package/scaffold/applet/src/client/components/ui/separator.tsx +25 -0
- package/scaffold/applet/src/client/components/ui/sheet.tsx +177 -0
- package/scaffold/applet/src/client/components/ui/skeleton.tsx +13 -0
- package/scaffold/applet/src/client/components/ui/spinner.tsx +16 -0
- package/scaffold/applet/src/client/components/ui/switch.tsx +41 -0
- package/scaffold/applet/src/client/components/ui/table.tsx +114 -0
- package/scaffold/applet/src/client/components/ui/tabs.tsx +78 -0
- package/scaffold/applet/src/client/components/ui/textarea.tsx +23 -0
- package/scaffold/applet/src/client/lib/utils.ts +6 -0
- package/scaffold/applet/src/client/styles/globals.css +136 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hitch42 Pty Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PersistQueryClientOptions } from "@tanstack/react-query-persist-client";
|
|
2
|
+
import { AnyRouter } from "@tanstack/react-router";
|
|
3
|
+
//#region src/internal/client.d.ts
|
|
4
|
+
declare function appletQueryPersistence(appletCode: string): Promise<Omit<PersistQueryClientOptions, "queryClient"> | undefined>;
|
|
5
|
+
declare function mountApplet(router: AnyRouter, appletCode: string): void;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { appletQueryPersistence, mountApplet };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createAsyncStoragePersister } from "@tanstack/query-async-storage-persister";
|
|
2
|
+
import { QueryClientProvider } from "@tanstack/react-query";
|
|
3
|
+
import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client";
|
|
4
|
+
import { RouterProvider } from "@tanstack/react-router";
|
|
5
|
+
import { createElement } from "react";
|
|
6
|
+
import { createRoot } from "react-dom/client";
|
|
7
|
+
//#region src/internal/client.ts
|
|
8
|
+
const CACHE_MAX_AGE = 3e5;
|
|
9
|
+
const CACHE_SCOPE_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
10
|
+
async function appletQueryPersistence(appletCode) {
|
|
11
|
+
const contextPromise = window.__hitchCacheContext;
|
|
12
|
+
if (!contextPromise) return void 0;
|
|
13
|
+
let context;
|
|
14
|
+
try {
|
|
15
|
+
context = await contextPromise;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error("Could not load the applet cache context.", error);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const cacheScope = typeof context === "object" && context !== null && "cacheScope" in context && typeof context.cacheScope === "string" ? context.cacheScope.trim() : void 0;
|
|
21
|
+
const cacheBuster = typeof context === "object" && context !== null && "cacheBuster" in context && typeof context.cacheBuster === "string" ? context.cacheBuster.trim() : void 0;
|
|
22
|
+
if (!cacheScope || !CACHE_SCOPE_PATTERN.test(cacheScope) || !cacheBuster || !CACHE_SCOPE_PATTERN.test(cacheBuster)) return;
|
|
23
|
+
try {
|
|
24
|
+
return {
|
|
25
|
+
persister: createAsyncStoragePersister({
|
|
26
|
+
storage: window.sessionStorage,
|
|
27
|
+
key: `hitch:query:${appletCode}:${cacheScope}`,
|
|
28
|
+
throttleTime: 100,
|
|
29
|
+
retry: ({ error }) => {
|
|
30
|
+
console.error("Could not persist the applet query cache.", error);
|
|
31
|
+
}
|
|
32
|
+
}),
|
|
33
|
+
maxAge: CACHE_MAX_AGE,
|
|
34
|
+
buster: cacheBuster,
|
|
35
|
+
dehydrateOptions: { shouldDehydrateMutation: () => false }
|
|
36
|
+
};
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error("Could not access applet query cache storage.", error);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function mountApplet(router, appletCode) {
|
|
43
|
+
const queryClient = router.options.context.queryClient;
|
|
44
|
+
if (!queryClient) throw new Error("getRouter must provide a QueryClient via createRouter({ context: { queryClient } }).");
|
|
45
|
+
const root = document.getElementById("root");
|
|
46
|
+
if (root === null) throw new Error("Missing #root element in index.html.");
|
|
47
|
+
const applet = createElement(RouterProvider, { router });
|
|
48
|
+
appletQueryPersistence(appletCode).then((persistence) => {
|
|
49
|
+
createRoot(root).render(persistence ? createElement(PersistQueryClientProvider, {
|
|
50
|
+
client: queryClient,
|
|
51
|
+
persistOptions: persistence,
|
|
52
|
+
onError: () => console.error("Could not restore the applet query cache.")
|
|
53
|
+
}, applet) : createElement(QueryClientProvider, { client: queryClient }, applet));
|
|
54
|
+
}).catch((error) => {
|
|
55
|
+
console.error("Could not initialize the applet query cache.", error);
|
|
56
|
+
createRoot(root).render(createElement(QueryClientProvider, { client: queryClient }, applet));
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { appletQueryPersistence, mountApplet };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { t as AnyWorkflowDefinition } from "../workflow-DurM6Fwx.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/server/entry.d.ts
|
|
4
|
+
declare function createWorkerEntry(server: {
|
|
5
|
+
router: object;
|
|
6
|
+
workflows?: Record<string, AnyWorkflowDefinition>;
|
|
7
|
+
}, options: {
|
|
8
|
+
code: string;
|
|
9
|
+
}): {
|
|
10
|
+
fetch(request: Request, env?: unknown, ctx?: unknown): Promise<Response>;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/server/workflow-protocol.d.ts
|
|
14
|
+
type StepJournalEntry = {
|
|
15
|
+
kind: "step";
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
result: unknown;
|
|
19
|
+
};
|
|
20
|
+
type SleepJournalEntry = {
|
|
21
|
+
kind: "sleep";
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
until: number;
|
|
25
|
+
};
|
|
26
|
+
type AttemptJournalEntry = {
|
|
27
|
+
kind: "attempt";
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
error: string;
|
|
31
|
+
};
|
|
32
|
+
type FailedJournalEntry = {
|
|
33
|
+
kind: "failed";
|
|
34
|
+
id: string;
|
|
35
|
+
name: string;
|
|
36
|
+
error: string;
|
|
37
|
+
};
|
|
38
|
+
declare const journalEntrySchema: z.ZodType<StepJournalEntry | SleepJournalEntry | AttemptJournalEntry | FailedJournalEntry>;
|
|
39
|
+
type JournalEntry = z.infer<typeof journalEntrySchema>;
|
|
40
|
+
declare const workflowInvocationRequestSchema: z.ZodType<{
|
|
41
|
+
payload: unknown;
|
|
42
|
+
journal: JournalEntry[];
|
|
43
|
+
}>;
|
|
44
|
+
type WorkflowInvocationRequest = z.infer<typeof workflowInvocationRequestSchema>;
|
|
45
|
+
type StepOutcome = {
|
|
46
|
+
kind: "step";
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
49
|
+
result: unknown;
|
|
50
|
+
};
|
|
51
|
+
type SleepOutcome = {
|
|
52
|
+
kind: "sleep";
|
|
53
|
+
id: string;
|
|
54
|
+
name: string;
|
|
55
|
+
until: number;
|
|
56
|
+
};
|
|
57
|
+
type StepErrorOutcome = {
|
|
58
|
+
kind: "step_error";
|
|
59
|
+
id: string;
|
|
60
|
+
name: string;
|
|
61
|
+
message: string;
|
|
62
|
+
retries: {
|
|
63
|
+
limit: number;
|
|
64
|
+
delayMs: number;
|
|
65
|
+
backoff: "constant" | "linear" | "exponential";
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
type DoneOutcome = {
|
|
69
|
+
kind: "done";
|
|
70
|
+
result: unknown;
|
|
71
|
+
};
|
|
72
|
+
type ErrorOutcome = {
|
|
73
|
+
kind: "error";
|
|
74
|
+
message: string;
|
|
75
|
+
};
|
|
76
|
+
declare const workflowOutcomeSchema: z.ZodType<StepOutcome | SleepOutcome | StepErrorOutcome | DoneOutcome | ErrorOutcome>;
|
|
77
|
+
type WorkflowOutcome = z.infer<typeof workflowOutcomeSchema>;
|
|
78
|
+
//#endregion
|
|
79
|
+
export { type JournalEntry, type WorkflowInvocationRequest, type WorkflowOutcome, createWorkerEntry, journalEntrySchema, workflowInvocationRequestSchema, workflowOutcomeSchema };
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { o as runWithRequestContext, r as toMilliseconds, t as MAX_DURATION_MS } from "../workflow-CTo2kZF1.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { RPCHandler } from "@orpc/server/fetch";
|
|
4
|
+
//#region src/headers.ts
|
|
5
|
+
const HITCH_USER_HEADER = "x-hitch-user";
|
|
6
|
+
const HITCH_SESSION_HEADER = "x-hitch-session";
|
|
7
|
+
const HITCH_APPLET_HEADER = "x-hitch-applet";
|
|
8
|
+
const HITCH_APPLET_TOKEN_HEADER = "x-hitch-applet-token";
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/server/workflow-protocol.ts
|
|
11
|
+
const journalEntrySchema = z.discriminatedUnion("kind", [
|
|
12
|
+
z.object({
|
|
13
|
+
kind: z.literal("step"),
|
|
14
|
+
id: z.string(),
|
|
15
|
+
name: z.string(),
|
|
16
|
+
result: z.unknown()
|
|
17
|
+
}),
|
|
18
|
+
z.object({
|
|
19
|
+
kind: z.literal("sleep"),
|
|
20
|
+
id: z.string(),
|
|
21
|
+
name: z.string(),
|
|
22
|
+
until: z.number()
|
|
23
|
+
}),
|
|
24
|
+
z.object({
|
|
25
|
+
kind: z.literal("attempt"),
|
|
26
|
+
id: z.string(),
|
|
27
|
+
name: z.string(),
|
|
28
|
+
error: z.string()
|
|
29
|
+
}),
|
|
30
|
+
z.object({
|
|
31
|
+
kind: z.literal("failed"),
|
|
32
|
+
id: z.string(),
|
|
33
|
+
name: z.string(),
|
|
34
|
+
error: z.string()
|
|
35
|
+
})
|
|
36
|
+
]);
|
|
37
|
+
const workflowInvocationRequestSchema = z.object({
|
|
38
|
+
payload: z.unknown(),
|
|
39
|
+
journal: z.array(journalEntrySchema)
|
|
40
|
+
});
|
|
41
|
+
const workflowOutcomeSchema = z.discriminatedUnion("kind", [
|
|
42
|
+
z.object({
|
|
43
|
+
kind: z.literal("step"),
|
|
44
|
+
id: z.string(),
|
|
45
|
+
name: z.string(),
|
|
46
|
+
result: z.unknown()
|
|
47
|
+
}),
|
|
48
|
+
z.object({
|
|
49
|
+
kind: z.literal("sleep"),
|
|
50
|
+
id: z.string(),
|
|
51
|
+
name: z.string(),
|
|
52
|
+
until: z.number()
|
|
53
|
+
}),
|
|
54
|
+
z.object({
|
|
55
|
+
kind: z.literal("step_error"),
|
|
56
|
+
id: z.string(),
|
|
57
|
+
name: z.string(),
|
|
58
|
+
message: z.string(),
|
|
59
|
+
retries: z.object({
|
|
60
|
+
limit: z.number(),
|
|
61
|
+
delayMs: z.number(),
|
|
62
|
+
backoff: z.enum([
|
|
63
|
+
"constant",
|
|
64
|
+
"linear",
|
|
65
|
+
"exponential"
|
|
66
|
+
])
|
|
67
|
+
})
|
|
68
|
+
}),
|
|
69
|
+
z.object({
|
|
70
|
+
kind: z.literal("done"),
|
|
71
|
+
result: z.unknown()
|
|
72
|
+
}),
|
|
73
|
+
z.object({
|
|
74
|
+
kind: z.literal("error"),
|
|
75
|
+
message: z.string()
|
|
76
|
+
})
|
|
77
|
+
]);
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/server/workflow-run.ts
|
|
80
|
+
const MAX_RESULT_SIZE = 1048576;
|
|
81
|
+
function errorMessage(error) {
|
|
82
|
+
return (error instanceof Error ? error.message : String(error)).slice(0, 2048);
|
|
83
|
+
}
|
|
84
|
+
function outcomeTooLarge(outcome) {
|
|
85
|
+
return new TextEncoder().encode(JSON.stringify(outcome)).byteLength > MAX_RESULT_SIZE;
|
|
86
|
+
}
|
|
87
|
+
async function runWorkflowInvocation(definition, request) {
|
|
88
|
+
const occurrences = /* @__PURE__ */ new Map();
|
|
89
|
+
const never = new Promise(() => void 0);
|
|
90
|
+
let claimed = false;
|
|
91
|
+
let resolveRecorded = () => void 0;
|
|
92
|
+
const recordedPromise = new Promise((resolve) => {
|
|
93
|
+
resolveRecorded = resolve;
|
|
94
|
+
});
|
|
95
|
+
const nextId = (name) => {
|
|
96
|
+
const occurrence = (occurrences.get(name) ?? 0) + 1;
|
|
97
|
+
occurrences.set(name, occurrence);
|
|
98
|
+
return `${name}#${occurrence}`;
|
|
99
|
+
};
|
|
100
|
+
const record = (outcome) => {
|
|
101
|
+
resolveRecorded(outcome);
|
|
102
|
+
return never;
|
|
103
|
+
};
|
|
104
|
+
const runPromise = definition.run({
|
|
105
|
+
async do(name, optionsOrFn, possibleFn) {
|
|
106
|
+
const id = nextId(name);
|
|
107
|
+
const journaled = request.journal.find((entry) => entry.kind === "step" && entry.id === id);
|
|
108
|
+
if (journaled?.kind === "step") return journaled.result;
|
|
109
|
+
const failed = request.journal.find((entry) => entry.kind === "failed" && entry.id === id);
|
|
110
|
+
if (failed?.kind === "failed") throw new Error(failed.error);
|
|
111
|
+
if (claimed) return never;
|
|
112
|
+
claimed = true;
|
|
113
|
+
const options = typeof optionsOrFn === "function" ? void 0 : optionsOrFn;
|
|
114
|
+
const fn = typeof optionsOrFn === "function" ? optionsOrFn : possibleFn;
|
|
115
|
+
const retries = options?.retries ? {
|
|
116
|
+
limit: options.retries.limit,
|
|
117
|
+
delayMs: toMilliseconds(options.retries.delay),
|
|
118
|
+
backoff: options.retries.backoff ?? "constant"
|
|
119
|
+
} : {
|
|
120
|
+
limit: 0,
|
|
121
|
+
delayMs: 0,
|
|
122
|
+
backoff: "constant"
|
|
123
|
+
};
|
|
124
|
+
if (retries.delayMs * (retries.backoff === "exponential" ? 2 ** (retries.limit - 1) : retries.backoff === "linear" ? retries.limit : 1) > MAX_DURATION_MS) throw new Error("Retry delay grows beyond 365 days.");
|
|
125
|
+
try {
|
|
126
|
+
const outcome = {
|
|
127
|
+
kind: "step",
|
|
128
|
+
id,
|
|
129
|
+
name,
|
|
130
|
+
result: await fn()
|
|
131
|
+
};
|
|
132
|
+
return record(outcomeTooLarge(outcome) ? {
|
|
133
|
+
kind: "error",
|
|
134
|
+
message: `Step "${name}" result exceeds 1 MiB.`
|
|
135
|
+
} : outcome);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return record({
|
|
138
|
+
kind: "step_error",
|
|
139
|
+
id,
|
|
140
|
+
name,
|
|
141
|
+
message: errorMessage(error),
|
|
142
|
+
retries
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
async sleep(name, duration) {
|
|
147
|
+
const id = nextId(name);
|
|
148
|
+
if (request.journal.find((entry) => entry.kind === "sleep" && entry.id === id) !== void 0) return;
|
|
149
|
+
if (claimed) return never;
|
|
150
|
+
claimed = true;
|
|
151
|
+
return record({
|
|
152
|
+
kind: "sleep",
|
|
153
|
+
id,
|
|
154
|
+
name,
|
|
155
|
+
until: Date.now() + toMilliseconds(duration)
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}, request.payload).then((result) => {
|
|
159
|
+
const outcome = {
|
|
160
|
+
kind: "done",
|
|
161
|
+
result
|
|
162
|
+
};
|
|
163
|
+
return outcomeTooLarge(outcome) ? {
|
|
164
|
+
kind: "error",
|
|
165
|
+
message: "Workflow result exceeds 1 MiB."
|
|
166
|
+
} : outcome;
|
|
167
|
+
}, (error) => ({
|
|
168
|
+
kind: "error",
|
|
169
|
+
message: errorMessage(error)
|
|
170
|
+
}));
|
|
171
|
+
return Promise.race([runPromise, recordedPromise]);
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/server/entry.ts
|
|
175
|
+
const WORKFLOW_PATH_PREFIX = "/api/__hitch/workflows/";
|
|
176
|
+
const SERVER_EXPORT_ERROR = "src/server/main.ts must default-export { router, workflows } — change \"export default router\" to \"export default { router }\".";
|
|
177
|
+
function jsonError(status, error) {
|
|
178
|
+
return Response.json({ error }, { status });
|
|
179
|
+
}
|
|
180
|
+
function createWorkerEntry(server, options) {
|
|
181
|
+
const candidate = server;
|
|
182
|
+
if (typeof candidate !== "object" || candidate === null || !("router" in candidate)) throw new Error(SERVER_EXPORT_ERROR);
|
|
183
|
+
const handler = new RPCHandler(server.router);
|
|
184
|
+
return { async fetch(request, env) {
|
|
185
|
+
const userId = request.headers.get(HITCH_USER_HEADER);
|
|
186
|
+
const sessionToken = request.headers.get(HITCH_SESSION_HEADER);
|
|
187
|
+
const appletId = request.headers.get(HITCH_APPLET_HEADER);
|
|
188
|
+
const token = request.headers.get(HITCH_APPLET_TOKEN_HEADER);
|
|
189
|
+
const identity = userId !== null && sessionToken !== null ? {
|
|
190
|
+
kind: "user",
|
|
191
|
+
userId,
|
|
192
|
+
sessionToken
|
|
193
|
+
} : appletId !== null && token !== null ? {
|
|
194
|
+
kind: "applet",
|
|
195
|
+
appletId,
|
|
196
|
+
token
|
|
197
|
+
} : void 0;
|
|
198
|
+
if (identity === void 0) return jsonError(401, "missing_identity");
|
|
199
|
+
const bindings = typeof env === "object" && env !== null ? env : {};
|
|
200
|
+
if (typeof bindings.HITCH_API_URL !== "string") return jsonError(500, "missing_api_binding");
|
|
201
|
+
const service = typeof bindings.HITCH === "object" && bindings.HITCH !== null ? bindings.HITCH : void 0;
|
|
202
|
+
const apiFetch = typeof service?.fetch === "function" ? service.fetch.bind(bindings.HITCH) : globalThis.fetch.bind(globalThis);
|
|
203
|
+
return runWithRequestContext({
|
|
204
|
+
identity,
|
|
205
|
+
appletCode: options.code,
|
|
206
|
+
api: {
|
|
207
|
+
baseUrl: bindings.HITCH_API_URL,
|
|
208
|
+
fetch: apiFetch
|
|
209
|
+
}
|
|
210
|
+
}, async () => {
|
|
211
|
+
const pathname = new URL(request.url).pathname;
|
|
212
|
+
if (pathname.startsWith(WORKFLOW_PATH_PREFIX)) {
|
|
213
|
+
const parsed = await request.json().then((body) => workflowInvocationRequestSchema.safeParse(body)).catch(() => void 0);
|
|
214
|
+
if (parsed === void 0 || !parsed.success) return jsonError(400, "malformed_request");
|
|
215
|
+
const code = decodeURIComponent(pathname.slice(23));
|
|
216
|
+
const workflows = server.workflows ?? {};
|
|
217
|
+
const definition = Object.hasOwn(workflows, code) ? workflows[code] : void 0;
|
|
218
|
+
return Response.json(definition === void 0 ? {
|
|
219
|
+
kind: "error",
|
|
220
|
+
message: `Unknown workflow "${code}".`
|
|
221
|
+
} : await runWorkflowInvocation(definition, parsed.data));
|
|
222
|
+
}
|
|
223
|
+
const result = await handler.handle(request, {
|
|
224
|
+
context: {},
|
|
225
|
+
prefix: "/api"
|
|
226
|
+
});
|
|
227
|
+
return result.matched ? result.response : new Response(null, { status: 404 });
|
|
228
|
+
});
|
|
229
|
+
} };
|
|
230
|
+
}
|
|
231
|
+
//#endregion
|
|
232
|
+
export { createWorkerEntry, journalEntrySchema, workflowInvocationRequestSchema, workflowOutcomeSchema };
|
package/dist/applet.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/applet.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
//#region bin/applet.ts
|
|
4
|
+
const usage = `Usage:
|
|
5
|
+
applet init <org> [--no-commit | --no-git] [--no-install]
|
|
6
|
+
applet bootstrap [--no-install]
|
|
7
|
+
applet new <code> [--client-only | --server-only] [--no-install]
|
|
8
|
+
applet generate [--changeset <id>]
|
|
9
|
+
applet build [--changeset <id>]
|
|
10
|
+
applet deploy [--changeset <id>]
|
|
11
|
+
applet logs [code] [--since <duration>] [--limit <count>]
|
|
12
|
+
[--search <text>] [--level <level>] [--filter <json>] [--run <id>] [--live]
|
|
13
|
+
applet variables set <KEY> [value] [--secret] [-y | --yes]
|
|
14
|
+
applet variables list
|
|
15
|
+
applet variables delete <KEY> [-y | --yes]
|
|
16
|
+
|
|
17
|
+
Init options:
|
|
18
|
+
--no-commit Do not create the initial commit
|
|
19
|
+
--no-git Do not create a git repository
|
|
20
|
+
--no-install Do not run pnpm install
|
|
21
|
+
|
|
22
|
+
New options:
|
|
23
|
+
--client-only Create only client source
|
|
24
|
+
--server-only Create only server source
|
|
25
|
+
--no-install Do not run pnpm install
|
|
26
|
+
|
|
27
|
+
Bootstrap options:
|
|
28
|
+
--no-install Do not run pnpm install
|
|
29
|
+
|
|
30
|
+
Generate options:
|
|
31
|
+
--changeset <id> Generate against the schema this pending changeset
|
|
32
|
+
would produce. The build honours HITCH_CHANGESET_ID.
|
|
33
|
+
|
|
34
|
+
Deploy options:
|
|
35
|
+
--changeset <id> Deploy against a pending schema changeset. The build
|
|
36
|
+
generates against its schema and the release promotes
|
|
37
|
+
when the changeset applies. Manifest mismatches warn.
|
|
38
|
+
|
|
39
|
+
Logs options:
|
|
40
|
+
--since <duration> Show logs from this period. Default: 1h. Maximum: 7d.
|
|
41
|
+
--limit <count> Return up to this many logs. Default: 100. Maximum: 100.
|
|
42
|
+
--search <text> Search log text.
|
|
43
|
+
--level <level> Show this level and higher levels.
|
|
44
|
+
Valid levels: debug, log, info, warn, error.
|
|
45
|
+
The log level is an alias for info.
|
|
46
|
+
--run <id> Show workflow logs for one run.
|
|
47
|
+
--filter <json> Filter log fields with a Hitch JSON filter object.
|
|
48
|
+
--live Poll for new logs every two seconds.
|
|
49
|
+
|
|
50
|
+
Variables options:
|
|
51
|
+
--secret Store the value as a secret.
|
|
52
|
+
-y, --yes Skip the live applet confirmation.
|
|
53
|
+
|
|
54
|
+
CI: set HITCH_API_KEY (and HITCH_API_URL for a non-default instance).`;
|
|
55
|
+
async function main() {
|
|
56
|
+
const { positionals, values } = parseArgs({
|
|
57
|
+
allowPositionals: true,
|
|
58
|
+
options: {
|
|
59
|
+
changeset: { type: "string" },
|
|
60
|
+
"client-only": { type: "boolean" },
|
|
61
|
+
filter: { type: "string" },
|
|
62
|
+
run: { type: "string" },
|
|
63
|
+
level: { type: "string" },
|
|
64
|
+
limit: { type: "string" },
|
|
65
|
+
live: { type: "boolean" },
|
|
66
|
+
"no-commit": { type: "boolean" },
|
|
67
|
+
"no-git": { type: "boolean" },
|
|
68
|
+
"no-install": { type: "boolean" },
|
|
69
|
+
secret: { type: "boolean" },
|
|
70
|
+
search: { type: "string" },
|
|
71
|
+
"server-only": { type: "boolean" },
|
|
72
|
+
since: { type: "string" },
|
|
73
|
+
yes: {
|
|
74
|
+
type: "boolean",
|
|
75
|
+
short: "y"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
strict: true
|
|
79
|
+
});
|
|
80
|
+
const noGitFlags = values["no-commit"] === void 0 && values["no-git"] === void 0;
|
|
81
|
+
const noVariableFlags = values.secret === void 0 && values.yes === void 0;
|
|
82
|
+
const noLogFlags = values.run === void 0 && values.filter === void 0 && values.level === void 0 && values.limit === void 0 && values.live === void 0 && values.search === void 0 && values.since === void 0;
|
|
83
|
+
const noModeFlags = values["client-only"] === void 0 && values["server-only"] === void 0;
|
|
84
|
+
if (positionals.length === 1 && positionals[0] === "bootstrap" && values.changeset === void 0 && noModeFlags && noGitFlags && noVariableFlags && noLogFlags) {
|
|
85
|
+
await (await import("./init-BS1Y-eNL.js")).runBootstrap({ install: values["no-install"] !== true });
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (positionals.length === 1 && positionals[0] === "generate" && values["no-install"] === void 0 && noModeFlags && noGitFlags && noVariableFlags && noLogFlags) {
|
|
89
|
+
await (await import("./generate-6Nbt7uii.js")).runGenerate({ changesetId: values.changeset });
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (positionals.length === 1 && positionals[0] === "build" && values["no-install"] === void 0 && noModeFlags && noGitFlags && noVariableFlags && noLogFlags) {
|
|
93
|
+
await (await import("./build-CEku1lyb.js")).runBuild({ changesetId: values.changeset });
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (positionals.length === 1 && positionals[0] === "deploy" && values["no-install"] === void 0 && noModeFlags && noGitFlags && noVariableFlags && noLogFlags) {
|
|
97
|
+
await (await import("./deploy-D3BGDqgC.js")).runDeploy({ changesetId: values.changeset });
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (positionals.length === 2 && positionals[0] === "init" && values.changeset === void 0 && noModeFlags && noVariableFlags && noLogFlags) {
|
|
101
|
+
await (await import("./init-BS1Y-eNL.js")).runInit(positionals[1], {
|
|
102
|
+
commit: values["no-commit"] !== true,
|
|
103
|
+
git: values["no-git"] !== true,
|
|
104
|
+
install: values["no-install"] !== true
|
|
105
|
+
});
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (positionals.length === 2 && positionals[0] === "new" && values.changeset === void 0 && noGitFlags && noVariableFlags && noLogFlags) {
|
|
109
|
+
await (await import("./init-BS1Y-eNL.js")).runNew({
|
|
110
|
+
code: positionals[1],
|
|
111
|
+
clientOnly: values["client-only"],
|
|
112
|
+
install: values["no-install"] !== true,
|
|
113
|
+
serverOnly: values["server-only"]
|
|
114
|
+
});
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if ((positionals.length === 1 || positionals.length === 2) && positionals[0] === "logs" && values.changeset === void 0 && values["no-install"] === void 0 && noModeFlags && noGitFlags && noVariableFlags) {
|
|
118
|
+
await (await import("./logs-m00pzr_O.js")).runLogs({
|
|
119
|
+
code: positionals[1],
|
|
120
|
+
filter: values.filter,
|
|
121
|
+
run: values.run,
|
|
122
|
+
level: values.level,
|
|
123
|
+
limit: values.limit,
|
|
124
|
+
live: values.live,
|
|
125
|
+
search: values.search,
|
|
126
|
+
since: values.since
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if ((positionals.length === 3 || positionals.length === 4) && positionals[0] === "variables" && values.changeset === void 0 && positionals[1] === "set" && values["no-install"] === void 0 && noModeFlags && noGitFlags && noLogFlags) {
|
|
131
|
+
await (await import("./variables-CP7Ce3Np.js")).runVariablesSet({
|
|
132
|
+
key: positionals[2],
|
|
133
|
+
secret: values.secret,
|
|
134
|
+
value: positionals[3],
|
|
135
|
+
yes: values.yes
|
|
136
|
+
});
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (positionals.length === 2 && positionals[0] === "variables" && values.changeset === void 0 && positionals[1] === "list" && values["no-install"] === void 0 && noModeFlags && noGitFlags && noVariableFlags && noLogFlags) {
|
|
140
|
+
await (await import("./variables-CP7Ce3Np.js")).runVariablesList({});
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (positionals.length === 3 && positionals[0] === "variables" && values.changeset === void 0 && positionals[1] === "delete" && values["no-install"] === void 0 && noModeFlags && noGitFlags && values.secret === void 0 && noLogFlags) {
|
|
144
|
+
await (await import("./variables-CP7Ce3Np.js")).runVariablesDelete({
|
|
145
|
+
key: positionals[2],
|
|
146
|
+
yes: values.yes
|
|
147
|
+
});
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
throw new Error(usage);
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
await main();
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error(error instanceof Error ? error.message : error);
|
|
156
|
+
if (!(error instanceof Error) || error.message !== usage) console.error(`\n${usage}`);
|
|
157
|
+
process.exitCode = 1;
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
160
|
+
export {};
|