moi-computer 0.11.1-next.2 → 0.11.1-next.3
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/README.md +2 -2
- package/client/app/AppRouter.tsx +2 -2
- package/client/app/routes/WorkspaceRoute.tsx +7 -9
- package/client/app/shell/SidebarLayout.tsx +18 -15
- package/client/components/shared/BottomPanel.tsx +26 -14
- package/client/components/shared/PanelHeader.tsx +1 -1
- package/client/components/shared/ReorderableList.tsx +7 -2
- package/client/components/shared/WorkspaceIcon.tsx +92 -0
- package/client/components/ui/alert-dialog.tsx +1 -0
- package/client/components/ui/checkbox.tsx +1 -0
- package/client/components/ui/inline-input.tsx +84 -0
- package/client/components/ui/toast.tsx +2 -2
- package/client/features/applets/AppletMount.tsx +1 -1
- package/client/features/applets/applet-cache.ts +3 -3
- package/client/features/applets/applet-runtime.ts +147 -108
- package/client/features/applets/applet-styles.ts +2 -2
- package/client/features/applets/applet-thumbnail.ts +6 -8
- package/client/features/applets/useApplet.ts +10 -9
- package/client/features/chat/ChatPanel.tsx +19 -10
- package/client/features/chat/ChatPopup.tsx +7 -2
- package/client/features/chat/applet-chat-intents.ts +170 -0
- package/client/features/chat/chat-send.ts +103 -62
- package/client/features/chat/chat-store.ts +50 -59
- package/client/features/chat/composer/ChatComposer.tsx +43 -132
- package/client/features/chat/composer/ModelPicker.tsx +1 -1
- package/client/features/chat/composer/attachments/AttachmentChip.tsx +99 -0
- package/client/features/chat/composer/attachments/DrawingAttachmentChip.tsx +37 -0
- package/client/features/chat/composer/attachments/FileAttachmentChip.tsx +22 -0
- package/client/features/chat/composer/attachments/TextAttachmentChip.tsx +11 -0
- package/client/features/chat/composer/attachments/draft-attachments.ts +168 -0
- package/client/features/chat/composer/attachments/types.ts +25 -0
- package/client/features/chat/composer/attachments/uploads.ts +26 -0
- package/client/features/chat/{chat-connection.ts → connection/chat-connection.ts} +1 -1
- package/client/features/chat/{chat-frames.ts → connection/chat-frames.ts} +2 -2
- package/client/features/chat/{ChatEmptyState.tsx → messages/ChatEmptyState.tsx} +16 -11
- package/client/features/chat/{MarkdownContent.tsx → messages/MarkdownContent.tsx} +16 -1
- package/client/features/chat/{TurnView.tsx → messages/TurnView.tsx} +22 -15
- package/client/features/chat/{tool-group → messages/tool-group}/SubagentCard.tsx +1 -1
- package/client/features/chat/{tool-group → messages/tool-group}/TimelineRow.tsx +2 -2
- package/client/features/chat/{tool-group → messages/tool-group}/ToolCallGroup.tsx +1 -1
- package/client/features/chat/{ChatSelector.tsx → sessions/ChatSelector.tsx} +3 -3
- package/client/features/chat/{api.ts → sessions/api.ts} +1 -1
- package/client/features/chat/useChat.ts +32 -15
- package/client/features/dev/ChatStatesPage.tsx +5 -6
- package/client/features/dev/HarnessDebugPage.tsx +10 -7
- package/client/features/dev/ToolCallsPage.tsx +1 -1
- package/client/features/dev/UiComponentPreviews.tsx +21 -0
- package/client/features/dev/chat-states-fixtures.ts +13 -2
- package/client/features/drawings/useChatAnnotation.ts +17 -17
- package/client/features/drawings/useDrawingLayer.ts +7 -9
- package/client/features/drawings/useViewBuilderSketch.ts +18 -15
- package/client/features/home/HomePage.tsx +8 -7
- package/client/features/home/WorkspacePreview.tsx +1 -1
- package/client/features/home/workspace-presentation.tsx +1 -11
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +2 -4
- package/client/features/overview/HiddenPanel.tsx +109 -0
- package/client/features/overview/Overview.tsx +302 -0
- package/client/features/overview/WidgetCanvas.tsx +164 -0
- package/client/features/{widgets → overview}/WidgetFrame.tsx +24 -16
- package/client/features/overview/WorkspaceName.tsx +60 -0
- package/client/features/overview/api.ts +30 -0
- package/client/features/overview/default/ViewsWidget.tsx +252 -0
- package/client/features/overview/default/registry.tsx +36 -0
- package/client/features/{widgets → overview}/grid.ts +36 -0
- package/client/features/overview/useGridReconcile.ts +94 -0
- package/client/features/scratchpad/Scratchpad.tsx +7 -8
- package/client/features/scratchpad/ScratchpadControls.tsx +0 -1
- package/client/features/scratchpad/useScratchpadSnapshot.tsx +23 -13
- package/client/features/settings/GeneralSettings.tsx +55 -58
- package/client/features/settings/SettingsLayout.tsx +2 -2
- package/client/features/settings/WorkspaceIconPicker.tsx +318 -0
- package/client/features/settings/WorkspaceSettingsDialog.tsx +65 -0
- package/client/features/settings/api.ts +48 -24
- package/client/features/views/ViewBuilder.tsx +5 -4
- package/client/features/views/ViewManager.tsx +11 -13
- package/client/features/views/api.ts +36 -2
- package/client/features/views/useViewBuilderActions.ts +8 -5
- package/client/features/views/useViewBuilderDrafts.ts +23 -21
- package/client/features/views/view-presentation.ts +13 -0
- package/client/features/workspace/{CustomizePanel.tsx → ThemePanel.tsx} +26 -31
- package/client/features/workspace/UnavailablePage.tsx +21 -0
- package/client/features/workspace/WorkspaceLayoutContext.tsx +4 -7
- package/client/features/workspace/WorkspaceScreen.tsx +205 -161
- package/client/features/workspace/WorkspaceSplitLayout.tsx +1 -1
- package/client/features/workspace/WorkspaceTabs.tsx +2 -0
- package/client/features/workspace/api.ts +0 -26
- package/client/features/workspace/moi-context.ts +8 -9
- package/client/features/workspace/tab-resolution.ts +10 -33
- package/client/features/workspace/useWorkspaceNavigation.ts +164 -101
- package/client/index.css +1 -1
- package/client/lib/app-icon-registry.ts +2 -0
- package/client/lib/use-latest-ref.ts +11 -0
- package/client/main.tsx +1 -1
- package/client/runtime/useWorkspaceEvents.ts +59 -14
- package/client/theme.css +5 -5
- package/dist/chunk-1pp135b4.js +1 -0
- package/dist/chunk-2gs8jjx4.css +1 -0
- package/dist/chunk-94m3q9ce.js +1 -0
- package/dist/chunk-azkjzc7a.js +68 -0
- package/dist/{chunk-w482ew2c.js → chunk-brr0zbeh.js} +73 -73
- package/dist/chunk-m1fvqssh.js +2 -0
- package/dist/chunk-p34hvqt3.js +1 -0
- package/dist/{chunk-4s15axjs.js → chunk-p83f0jnj.js} +2 -2
- package/dist/{chunk-j3wvxjr7.js → chunk-tzz101ea.js} +1 -1
- package/dist/chunk-xg01f6pn.js +10 -0
- package/dist/{chunk-53a7pxcz.js → chunk-zs4mte79.js} +1 -1
- package/dist/index.html +1 -1
- package/lib/app-icons.ts +1 -0
- package/lib/applet-selector.ts +24 -0
- package/lib/chat-title.ts +6 -1
- package/lib/default-widgets.ts +32 -0
- package/lib/format.ts +12 -1
- package/lib/message-attachments.ts +40 -0
- package/lib/moi-attachments.ts +204 -0
- package/lib/moi-context.ts +20 -35
- package/lib/navigation.ts +84 -0
- package/lib/types.ts +79 -24
- package/lib/workspace-icon.ts +20 -0
- package/lib/workspace-layout.ts +22 -2
- package/lib/workspace-tabs.ts +9 -33
- package/package.json +27 -27
- package/registry.json +13 -0
- package/server/AGENTS.md +1 -0
- package/server/api.ts +114 -37
- package/server/applet-typecheck.ts +95 -0
- package/server/{bundler → applets}/build-applet.ts +13 -246
- package/server/applets/config.ts +226 -0
- package/server/applets/dependencies.ts +206 -0
- package/server/{applets.ts → applets/index.ts} +15 -161
- package/server/applets/runtime/base.ts +4 -0
- package/server/applets/runtime/moi.ts +42 -0
- package/server/applets/runtime/rpc.ts +17 -0
- package/server/applets/view-source.ts +67 -0
- package/server/attachment-message.ts +95 -0
- package/server/cli.ts +163 -97
- package/server/control.ts +63 -42
- package/server/harness/README.md +4 -2
- package/server/harness/acp/adapter.ts +7 -21
- package/server/harness/acp/session.ts +31 -50
- package/server/harness/claude-code/NOTES.md +8 -2
- package/server/harness/claude-code/adapter.ts +8 -25
- package/server/harness/claude-code/index.ts +2 -0
- package/server/harness/claude-code/session.ts +42 -47
- package/server/harness/claude-code/sessions.ts +9 -4
- package/server/harness/codex/adapter.ts +8 -7
- package/server/harness/codex/index.ts +1 -0
- package/server/harness/codex/permissions.ts +1 -1
- package/server/harness/codex/session.ts +20 -47
- package/server/harness/hermes/NOTES.md +1 -1
- package/server/harness/hermes/index.ts +4 -0
- package/server/harness/openclaw/NOTES.md +1 -1
- package/server/harness/openclaw/adapter.ts +22 -4
- package/server/harness/openclaw/gateway.ts +5 -3
- package/server/harness/openclaw/session.ts +27 -27
- package/server/harness/types.ts +5 -3
- package/server/layout.ts +9 -14
- package/server/moi-scaffold.ts +21 -13
- package/server/navigation-relay.ts +110 -0
- package/server/registry.ts +8 -0
- package/server/selected-session.ts +24 -4
- package/server/skill-update.ts +5 -3
- package/server/skill-version.ts +4 -7
- package/server/skills-template.ts +48 -9
- package/server/tabs.ts +19 -33
- package/server/uploads.ts +0 -0
- package/server/view-builders.ts +18 -1
- package/server/views.ts +126 -6
- package/server/web.ts +35 -9
- package/server/widgets.ts +12 -5
- package/server/workspace-config.ts +13 -9
- package/ui-components/button.tsx +2 -2
- package/ui-components/docs/kbd.md +191 -0
- package/ui-components/dropdown-menu.tsx +1 -1
- package/ui-components/kbd.tsx +32 -0
- package/ui-components/slider.tsx +1 -1
- package/ui-components/switch.tsx +1 -1
- package/ui-components/tabs.tsx +1 -1
- package/workspace/.claude/skills/moi-workspace/SKILL.md +284 -228
- package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +100 -153
- package/workspace/.claude/skills/moi-workspace/references/INTENTS.md +156 -0
- package/client/features/chat/attachment-staging.ts +0 -153
- package/client/features/chat/uploads.ts +0 -18
- package/client/features/chat/useAppletChatMessage.ts +0 -68
- package/client/features/settings/IconPicker.tsx +0 -442
- package/client/features/settings/WorkspaceSettings.tsx +0 -95
- package/client/features/settings/render-icon.ts +0 -173
- package/client/features/widgets/HiddenPanel.tsx +0 -80
- package/client/features/widgets/WidgetGrid.tsx +0 -123
- package/client/features/widgets/Widgets.tsx +0 -262
- package/client/features/widgets/useGridReconcile.ts +0 -73
- package/dist/chunk-245wt82v.css +0 -1
- package/dist/chunk-87thzbtw.js +0 -2
- package/dist/chunk-a6jad1a6.js +0 -1
- package/dist/chunk-dghwstd2.js +0 -9
- package/dist/chunk-eg120aca.js +0 -1
- package/dist/chunk-j7hh2ab4.js +0 -70
- package/dist/chunk-y8eyahwx.js +0 -1
- /package/client/features/chat/{ChatPromptBubbles.tsx → messages/ChatPromptBubbles.tsx} +0 -0
- /package/client/features/chat/{ShimmerText.tsx → messages/ShimmerText.tsx} +0 -0
- /package/client/features/chat/{group-turns.ts → messages/group-turns.ts} +0 -0
- /package/client/features/chat/{interleave-notices.ts → messages/interleave-notices.ts} +0 -0
- /package/client/features/chat/{preview-turn.ts → messages/preview-turn.ts} +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/CodeBlock.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/Collapse.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/ReadImagePreview.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/ToolOutput.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/detect.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/fixtures.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/claude.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/codex.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/hermes.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/index.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/mcp.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/openclaw.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/shared.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/shell.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/index.ts +0 -0
- /package/client/features/chat/{useStickToBottom.ts → messages/useStickToBottom.ts} +0 -0
- /package/client/features/chat/{session-view.ts → sessions/session-view.ts} +0 -0
- /package/client/features/chat/{useSelectedSession.ts → sessions/useSelectedSession.ts} +0 -0
- /package/server/{bundler → applets}/applet-css.ts +0 -0
- /package/server/{bundler → applets}/build-worker.ts +0 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Local dependency analysis shared by rebuild checks and applet cleanup.
|
|
2
|
+
// Each analysis reads files once, follows relative imports only, and is bounded.
|
|
3
|
+
// Incomplete graphs mean rebuild for staleness checks and keep helpers for cleanup.
|
|
4
|
+
import { readdir } from 'node:fs/promises'
|
|
5
|
+
import { dirname, join, resolve } from 'node:path'
|
|
6
|
+
|
|
7
|
+
export const MODULE_FILE_RE = /\.[mc]?[jt]sx?$/
|
|
8
|
+
const TS_ONLY_FILE_RE = /\.[mc]?ts$/
|
|
9
|
+
const MAX_GRAPH_FILES = 256
|
|
10
|
+
|
|
11
|
+
export type ModuleDependencies = {
|
|
12
|
+
modifiedAt: number
|
|
13
|
+
imports: string[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type DependencyGraph = {
|
|
17
|
+
entrypoints: Set<string>
|
|
18
|
+
modules: Map<string, ModuleDependencies>
|
|
19
|
+
complete: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Source module names in a kind's directory: `*.tsx`/`*.ts` minus `.server.ts`
|
|
23
|
+
// and minus `_`-prefixed files (`_utils.tsx`) — those are shared modules for
|
|
24
|
+
// entries to import, never entry points themselves.
|
|
25
|
+
export async function scanSources(sourceDir: string): Promise<string[]> {
|
|
26
|
+
try {
|
|
27
|
+
const entries = await readdir(sourceDir)
|
|
28
|
+
return entries
|
|
29
|
+
.filter(f => /\.(tsx|ts)$/.test(f) && !f.startsWith('_') && !f.endsWith('.server.ts'))
|
|
30
|
+
.map(f => f.replace(/\.tsx?$/, ''))
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return []
|
|
33
|
+
throw error
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function resolveSource(sourceDir: string, name: string): Promise<string | null> {
|
|
38
|
+
for (const ext of ['.tsx', '.ts']) {
|
|
39
|
+
const path = join(sourceDir, `${name}${ext}`)
|
|
40
|
+
if (await Bun.file(path).exists()) return path
|
|
41
|
+
}
|
|
42
|
+
return null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Bun lexes static, re-export, side-effect, and literal dynamic imports.
|
|
46
|
+
// Type-only imports are erased; bare package specifiers are outside this local graph.
|
|
47
|
+
const IMPORT_SCANNERS = {
|
|
48
|
+
ts: new Bun.Transpiler({ loader: 'ts' }),
|
|
49
|
+
tsx: new Bun.Transpiler({ loader: 'tsx' })
|
|
50
|
+
}
|
|
51
|
+
export function scanRelativeImports(source: string, loader: 'ts' | 'tsx' = 'tsx'): string[] | null {
|
|
52
|
+
let imports: { path: string }[]
|
|
53
|
+
try {
|
|
54
|
+
imports = IMPORT_SCANNERS[loader].scanImports(source)
|
|
55
|
+
} catch {
|
|
56
|
+
return null
|
|
57
|
+
}
|
|
58
|
+
return imports.map(i => i.path).filter(p => /^\.\.?\//.test(p))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Stop as soon as a changed file is found when checking an existing bundle.
|
|
62
|
+
// Without modifiedSince, return the whole graph for reachability/cleanup.
|
|
63
|
+
export async function analyzeDependencies(
|
|
64
|
+
entrypoints: Iterable<string>,
|
|
65
|
+
modifiedSince = Infinity
|
|
66
|
+
): Promise<DependencyGraph> {
|
|
67
|
+
const graph: DependencyGraph = {
|
|
68
|
+
entrypoints: new Set([...entrypoints].map(path => resolve(path))),
|
|
69
|
+
modules: new Map(),
|
|
70
|
+
complete: true
|
|
71
|
+
}
|
|
72
|
+
const queue = [...graph.entrypoints]
|
|
73
|
+
while (queue.length > 0) {
|
|
74
|
+
const path = queue.pop()!
|
|
75
|
+
if (graph.modules.has(path)) continue
|
|
76
|
+
if (graph.modules.size >= MAX_GRAPH_FILES) {
|
|
77
|
+
graph.complete = false
|
|
78
|
+
break
|
|
79
|
+
}
|
|
80
|
+
const module: ModuleDependencies = { modifiedAt: 0, imports: [] }
|
|
81
|
+
graph.modules.set(path, module)
|
|
82
|
+
try {
|
|
83
|
+
const file = Bun.file(path)
|
|
84
|
+
if (!(await file.exists())) {
|
|
85
|
+
graph.complete = false
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
88
|
+
module.modifiedAt = file.lastModified
|
|
89
|
+
if (module.modifiedAt >= modifiedSince) {
|
|
90
|
+
graph.complete = false
|
|
91
|
+
break
|
|
92
|
+
}
|
|
93
|
+
if (!MODULE_FILE_RE.test(path)) continue
|
|
94
|
+
const specifiers = scanRelativeImports(
|
|
95
|
+
await file.text(),
|
|
96
|
+
TS_ONLY_FILE_RE.test(path) ? 'ts' : 'tsx'
|
|
97
|
+
)
|
|
98
|
+
if (!specifiers) {
|
|
99
|
+
graph.complete = false
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
for (const specifier of specifiers) {
|
|
103
|
+
const dependency = await resolveModuleImport(dirname(path), specifier)
|
|
104
|
+
if (!dependency) {
|
|
105
|
+
graph.complete = false
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
module.imports.push(dependency)
|
|
109
|
+
queue.push(dependency)
|
|
110
|
+
}
|
|
111
|
+
} catch {
|
|
112
|
+
graph.complete = false
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return graph
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Include unfinished applets and each applet's companion server module: a
|
|
119
|
+
// companion can serve RPC calls without an import in the client source.
|
|
120
|
+
export async function analyzeAppletDependencies(workspacePath: string): Promise<DependencyGraph> {
|
|
121
|
+
const entrypoints: string[] = []
|
|
122
|
+
for (const kind of ['views', 'widgets']) {
|
|
123
|
+
const directory = join(workspacePath, '.moi', kind)
|
|
124
|
+
for (const name of await scanSources(directory)) {
|
|
125
|
+
for (const extension of ['.tsx', '.ts', '.server.ts']) {
|
|
126
|
+
const path = join(directory, name + extension)
|
|
127
|
+
if (await Bun.file(path).exists()) entrypoints.push(path)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return analyzeDependencies(entrypoints)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Reachability is an in-memory walk over an already analyzed graph.
|
|
135
|
+
export function collectDependencies(
|
|
136
|
+
graph: DependencyGraph,
|
|
137
|
+
entrypoints: Iterable<string>
|
|
138
|
+
): Set<string> {
|
|
139
|
+
const files = new Set<string>()
|
|
140
|
+
const queue = [...entrypoints]
|
|
141
|
+
while (queue.length > 0) {
|
|
142
|
+
const path = queue.pop()!
|
|
143
|
+
if (files.has(path)) continue
|
|
144
|
+
files.add(path)
|
|
145
|
+
queue.push(...(graph.modules.get(path)?.imports ?? []))
|
|
146
|
+
}
|
|
147
|
+
return files
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Extensions Bun appends to an extensionless import, in its own preference
|
|
151
|
+
// order (`./data` finds `data.tsx` before `data.ts` before … `data.json`).
|
|
152
|
+
// Doubles as the directory-index set: `./helpers` → `helpers/index.jsx`.
|
|
153
|
+
const RESOLVE_EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.cjs', '.json', '.mts', '.cts']
|
|
154
|
+
|
|
155
|
+
// TypeScript's output-extension rewrite: an import written against the emitted
|
|
156
|
+
// file name resolves to the source that produces it. (`.cjs` → `.cts` is
|
|
157
|
+
// deliberately absent — Bun doesn't do that one.)
|
|
158
|
+
const TS_EXTENSION_REWRITES: Record<string, string[]> = {
|
|
159
|
+
'.js': ['.ts', '.tsx'],
|
|
160
|
+
'.jsx': ['.tsx'],
|
|
161
|
+
'.mjs': ['.mts']
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Resolve a relative import to the file the bundler will actually read. This
|
|
165
|
+
// mirrors Bun's resolution rather than delegating to `Bun.resolveSync`, which
|
|
166
|
+
// memoizes results for the life of the process: in the long-running server it
|
|
167
|
+
// keeps handing back the path of a dependency that has since been deleted or
|
|
168
|
+
// renamed, which is exactly the case this check has to catch. Every candidate
|
|
169
|
+
// is probed against the filesystem, in Bun's order — literal path, TS
|
|
170
|
+
// extension rewrite, appended extension, then directory (`package.json` main,
|
|
171
|
+
// else `index.*`). Returns null when nothing on disk answers the specifier.
|
|
172
|
+
async function resolveModuleImport(dir: string, specifier: string): Promise<string | null> {
|
|
173
|
+
const base = join(dir, specifier)
|
|
174
|
+
const candidates = [base]
|
|
175
|
+
|
|
176
|
+
// Extension of the final segment only — a dot in a directory name (`./v1.2/x`)
|
|
177
|
+
// is not one.
|
|
178
|
+
const ext = /\.[^./\\]+$/.exec(base)?.[0] ?? ''
|
|
179
|
+
for (const rewrite of TS_EXTENSION_REWRITES[ext] ?? []) {
|
|
180
|
+
candidates.push(base.slice(0, base.length - ext.length) + rewrite)
|
|
181
|
+
}
|
|
182
|
+
for (const e of RESOLVE_EXTENSIONS) candidates.push(base + e)
|
|
183
|
+
|
|
184
|
+
for (const candidate of candidates) {
|
|
185
|
+
if (await Bun.file(candidate).exists()) return candidate
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Directory import. `package.json` main wins over `index.*`, matching Bun;
|
|
189
|
+
// an unreadable or `main`-less manifest just falls through to the indexes.
|
|
190
|
+
const pkg: unknown = await Bun.file(join(base, 'package.json'))
|
|
191
|
+
.json()
|
|
192
|
+
.catch(() => null)
|
|
193
|
+
const main =
|
|
194
|
+
typeof pkg === 'object' && pkg !== null && 'main' in pkg && typeof pkg.main === 'string'
|
|
195
|
+
? pkg.main
|
|
196
|
+
: null
|
|
197
|
+
if (main) {
|
|
198
|
+
const mainPath = join(base, main)
|
|
199
|
+
if (await Bun.file(mainPath).exists()) return mainPath
|
|
200
|
+
}
|
|
201
|
+
for (const e of RESOLVE_EXTENSIONS) {
|
|
202
|
+
const index = join(base, `index${e}`)
|
|
203
|
+
if (await Bun.file(index).exists()) return index
|
|
204
|
+
}
|
|
205
|
+
return null
|
|
206
|
+
}
|
|
@@ -15,15 +15,15 @@
|
|
|
15
15
|
// workspace-agnostic.
|
|
16
16
|
import { realpathSync, statSync } from 'node:fs'
|
|
17
17
|
import { mkdir, readdir, rm } from 'node:fs/promises'
|
|
18
|
-
import {
|
|
18
|
+
import { join, resolve, sep } from 'path'
|
|
19
19
|
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
} from '
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
import { analyzeDependencies, resolveSource, scanSources } from './dependencies'
|
|
21
|
+
import type { AppletKind } from './build-applet'
|
|
22
|
+
import { buildAppletsInChild } from './build-worker'
|
|
23
|
+
import { APPLET_API_BASE_SENTINEL } from './runtime/base'
|
|
24
|
+
import { pruneAppletThumbnails } from '../thumbnails'
|
|
25
|
+
|
|
26
|
+
export { scanSources } from './dependencies'
|
|
27
27
|
|
|
28
28
|
export type AppletPaths = {
|
|
29
29
|
moiRoot: string
|
|
@@ -55,161 +55,13 @@ export function getAppletRevision(
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
-
// entries to import, never entry points themselves.
|
|
61
|
-
export async function scanSources(sourceDir: string): Promise<string[]> {
|
|
62
|
-
try {
|
|
63
|
-
const entries = await readdir(sourceDir)
|
|
64
|
-
return entries
|
|
65
|
-
.filter(f => /\.(tsx|ts)$/.test(f) && !f.startsWith('_') && !f.endsWith('.server.ts'))
|
|
66
|
-
.map(f => f.replace(/\.tsx?$/, ''))
|
|
67
|
-
} catch {
|
|
68
|
-
return []
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function resolveSource(sourceDir: string, name: string): Promise<string | null> {
|
|
73
|
-
for (const ext of ['.tsx', '.ts']) {
|
|
74
|
-
const path = join(sourceDir, `${name}${ext}`)
|
|
75
|
-
if (await Bun.file(path).exists()) return path
|
|
76
|
-
}
|
|
77
|
-
return null
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Backstop for pathological graphs: an applet wiring more distinct files than
|
|
81
|
-
// this is reported stale without walking further — stale-but-rebuilt is the
|
|
82
|
-
// safe degradation, and the cap keeps the per-bundle check bounded no matter
|
|
83
|
-
// what the imports look like. Counts every input, images and JSON included, so
|
|
84
|
-
// it sits well above what an asset-heavy applet reaches (the walk never enters
|
|
85
|
-
// node_modules); hitting it means something is off anyway.
|
|
86
|
-
const MAX_GRAPH_FILES = 256
|
|
87
|
-
|
|
88
|
-
// Files the walk keeps descending through — anything Bun treats as a module
|
|
89
|
-
// (`.ts`, `.mjs`, `.cts`, …). Everything else (`.json`, `.css`, images) is a
|
|
90
|
-
// leaf.
|
|
91
|
-
const MODULE_FILE_RE = /\.[mc]?[jt]sx?$/
|
|
92
|
-
// Which transpiler loader lexes a module's imports. Only `.ts`/`.mts`/`.cts`
|
|
93
|
-
// take the `ts` loader: angle-bracket casts (`<string>x`) parse there and are
|
|
94
|
-
// JSX everywhere else. `.js`/`.mjs`/`.cjs` go through `tsx`, which accepts both
|
|
95
|
-
// plain JS and the JSX some of them contain.
|
|
96
|
-
const TS_ONLY_FILE_RE = /\.[mc]?ts$/
|
|
97
|
-
|
|
98
|
-
// A bundle is stale if its entry `index.js` is missing, any file in its local
|
|
99
|
-
// import graph has an mtime >= the built entry's, or one of those imports no
|
|
100
|
-
// longer resolves to a file at all.
|
|
101
|
-
//
|
|
102
|
-
// The graph is one uniform walk over every RELATIVE import the entry reaches:
|
|
103
|
-
// shared `_utils.tsx`, `../lib/*.ts`, `./data.json`, `./logo.png`,
|
|
104
|
-
// `./db.server`. Each is resolved to a real path, mtime-checked, and descended
|
|
105
|
-
// into when it's a module — so an imported image and a transitively imported
|
|
106
|
-
// helper are the same case, not two mechanisms. Bare specifiers (node_modules)
|
|
107
|
-
// are not walked: after a dependency bump, `moi bundle --force`. The bundle
|
|
108
|
-
// itself is React-mode-agnostic (see buildApplet), so it needs no rebuild when
|
|
109
|
-
// the server switches between the development and production React.
|
|
110
|
-
//
|
|
111
|
-
// `.server.ts` modules are walked like anything else, which is deliberately
|
|
112
|
-
// MORE than the bundle needs: their code never ships to the client (only the
|
|
113
|
-
// export list, inlined as RPC stubs), so editing one — or anything it imports
|
|
114
|
-
// — cannot change a byte of `index.js`. We report stale anyway because 'built'
|
|
115
|
-
// is what drives the worker recycle downstream (see `reloadModules`, called
|
|
116
|
-
// from widgets.ts/views.ts for every rebuilt applet). Without it, editing a
|
|
117
|
-
// helper that only a server module imports would leave a live worker serving
|
|
118
|
-
// the old code with nothing to dislodge it. The redundant rebuild is the price
|
|
119
|
-
// of that signal; the alternative is a second staleness channel threaded
|
|
120
|
-
// through the build results.
|
|
58
|
+
// Server dependencies also trigger rebuilds so their function worker is
|
|
59
|
+
// recycled, even when the client bundle's RPC stubs would remain identical.
|
|
121
60
|
async function needsRebuild(buildDir: string, name: string, srcPath: string): Promise<boolean> {
|
|
122
61
|
const built = Bun.file(join(buildDir, name, 'index.js'))
|
|
123
62
|
if (!(await built.exists())) return true
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
const queue = [srcPath]
|
|
127
|
-
const visited = new Set<string>()
|
|
128
|
-
while (queue.length > 0) {
|
|
129
|
-
const path = queue.pop()!
|
|
130
|
-
if (visited.has(path)) continue
|
|
131
|
-
visited.add(path)
|
|
132
|
-
if (visited.size > MAX_GRAPH_FILES) return true
|
|
133
|
-
const file = Bun.file(path)
|
|
134
|
-
if (!(await file.exists())) continue
|
|
135
|
-
if (file.lastModified >= builtMtime) return true
|
|
136
|
-
// Leaves stop at the mtime check above — an asset, JSON, or CSS file can't
|
|
137
|
-
// import anything.
|
|
138
|
-
if (!MODULE_FILE_RE.test(path)) continue
|
|
139
|
-
const source = await file.text()
|
|
140
|
-
const dir = dirname(path)
|
|
141
|
-
for (const specifier of scanRelativeImports(
|
|
142
|
-
source,
|
|
143
|
-
TS_ONLY_FILE_RE.test(path) ? 'ts' : 'tsx'
|
|
144
|
-
)) {
|
|
145
|
-
const resolved = await resolveModuleImport(dir, specifier)
|
|
146
|
-
// Nothing on disk answers this import, so the next build will fail on it.
|
|
147
|
-
// Report stale rather than skip: a helper deleted since the last build
|
|
148
|
-
// would otherwise leave `moi bundle` serving the last good bundle and
|
|
149
|
-
// never surfacing the missing-import error.
|
|
150
|
-
if (!resolved) return true
|
|
151
|
-
queue.push(resolved)
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return false
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Extensions Bun appends to an extensionless import, in its own preference
|
|
158
|
-
// order (`./data` finds `data.tsx` before `data.ts` before … `data.json`).
|
|
159
|
-
// Doubles as the directory-index set: `./helpers` → `helpers/index.jsx`.
|
|
160
|
-
const RESOLVE_EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.cjs', '.json', '.mts', '.cts']
|
|
161
|
-
|
|
162
|
-
// TypeScript's output-extension rewrite: an import written against the emitted
|
|
163
|
-
// file name resolves to the source that produces it. (`.cjs` → `.cts` is
|
|
164
|
-
// deliberately absent — Bun doesn't do that one.)
|
|
165
|
-
const TS_EXTENSION_REWRITES: Record<string, string[]> = {
|
|
166
|
-
'.js': ['.ts', '.tsx'],
|
|
167
|
-
'.jsx': ['.tsx'],
|
|
168
|
-
'.mjs': ['.mts']
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Resolve a relative import to the file the bundler will actually read. This
|
|
172
|
-
// mirrors Bun's resolution rather than delegating to `Bun.resolveSync`, which
|
|
173
|
-
// memoizes results for the life of the process: in the long-running server it
|
|
174
|
-
// keeps handing back the path of a dependency that has since been deleted or
|
|
175
|
-
// renamed, which is exactly the case this check has to catch. Every candidate
|
|
176
|
-
// is probed against the filesystem, in Bun's order — literal path, TS
|
|
177
|
-
// extension rewrite, appended extension, then directory (`package.json` main,
|
|
178
|
-
// else `index.*`). Returns null when nothing on disk answers the specifier.
|
|
179
|
-
async function resolveModuleImport(dir: string, specifier: string): Promise<string | null> {
|
|
180
|
-
const base = join(dir, specifier)
|
|
181
|
-
const candidates = [base]
|
|
182
|
-
|
|
183
|
-
// Extension of the final segment only — a dot in a directory name (`./v1.2/x`)
|
|
184
|
-
// is not one.
|
|
185
|
-
const ext = /\.[^./\\]+$/.exec(base)?.[0] ?? ''
|
|
186
|
-
for (const rewrite of TS_EXTENSION_REWRITES[ext] ?? []) {
|
|
187
|
-
candidates.push(base.slice(0, base.length - ext.length) + rewrite)
|
|
188
|
-
}
|
|
189
|
-
for (const e of RESOLVE_EXTENSIONS) candidates.push(base + e)
|
|
190
|
-
|
|
191
|
-
for (const candidate of candidates) {
|
|
192
|
-
if (await Bun.file(candidate).exists()) return candidate
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Directory import. `package.json` main wins over `index.*`, matching Bun;
|
|
196
|
-
// an unreadable or `main`-less manifest just falls through to the indexes.
|
|
197
|
-
const pkg: unknown = await Bun.file(join(base, 'package.json'))
|
|
198
|
-
.json()
|
|
199
|
-
.catch(() => null)
|
|
200
|
-
const main =
|
|
201
|
-
typeof pkg === 'object' && pkg !== null && 'main' in pkg && typeof pkg.main === 'string'
|
|
202
|
-
? pkg.main
|
|
203
|
-
: null
|
|
204
|
-
if (main) {
|
|
205
|
-
const mainPath = join(base, main)
|
|
206
|
-
if (await Bun.file(mainPath).exists()) return mainPath
|
|
207
|
-
}
|
|
208
|
-
for (const e of RESOLVE_EXTENSIONS) {
|
|
209
|
-
const index = join(base, `index${e}`)
|
|
210
|
-
if (await Bun.file(index).exists()) return index
|
|
211
|
-
}
|
|
212
|
-
return null
|
|
63
|
+
const graph = await analyzeDependencies([srcPath], built.lastModified)
|
|
64
|
+
return !graph.complete
|
|
213
65
|
}
|
|
214
66
|
|
|
215
67
|
// Built applet names: subdirectories of `buildDir` that hold an `index.js`
|
|
@@ -580,7 +432,8 @@ export type AppletBuildResult<C> = {
|
|
|
580
432
|
export async function buildApplets<C>(
|
|
581
433
|
workspacePath: string,
|
|
582
434
|
kind: AppletKind,
|
|
583
|
-
force: boolean
|
|
435
|
+
force: boolean,
|
|
436
|
+
target?: string
|
|
584
437
|
): Promise<{ names: string[]; results: AppletBuildResult<C>[]; ms: number }> {
|
|
585
438
|
const t0 = performance.now()
|
|
586
439
|
const { sourceDir, buildDir, moiRoot } = getAppletPaths(workspacePath, kind)
|
|
@@ -600,6 +453,7 @@ export async function buildApplets<C>(
|
|
|
600
453
|
|
|
601
454
|
const jobs = await Promise.all(
|
|
602
455
|
names.map(async name => {
|
|
456
|
+
if (target && name !== target) return { name, status: 'skipped' as const }
|
|
603
457
|
const srcPath = await resolveSource(sourceDir, name)
|
|
604
458
|
if (!srcPath) return { name, status: 'failed' as const, error: 'Source file not found' }
|
|
605
459
|
if (!force && !(await needsRebuild(buildDir, name, srcPath))) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AppletBridge, AttachmentInput } from '../../../lib/types'
|
|
2
|
+
|
|
3
|
+
import { APPLET_API_BASE_SENTINEL } from './base'
|
|
4
|
+
|
|
5
|
+
type AppletChatInput = {
|
|
6
|
+
message: string
|
|
7
|
+
attachments?: AttachmentInput[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const BASE = APPLET_API_BASE_SENTINEL
|
|
11
|
+
|
|
12
|
+
let bridge: Partial<AppletBridge> | null = null
|
|
13
|
+
|
|
14
|
+
export function __attachBridge(next: Partial<AppletBridge>): void {
|
|
15
|
+
bridge = next
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function __getBridge(): Partial<AppletBridge> | null {
|
|
19
|
+
return bridge
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function fileUrl(path: string): string {
|
|
23
|
+
const clean = String(path).replace(/^\/+/, '')
|
|
24
|
+
return BASE + '/fs/' + clean.split('/').map(encodeURIComponent).join('/')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function navigate(href: string): void {
|
|
28
|
+
bridge?.navigate?.(href)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolveHref(href: string): string {
|
|
32
|
+
return bridge?.resolveHref?.(href) ?? ''
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function addChatAttachment(input: AttachmentInput): void {
|
|
36
|
+
bridge?.addChatAttachment?.(input)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Keep positional calls working for previously built applets.
|
|
40
|
+
export function sendChatMessage(input: AppletChatInput | string, legacyContext?: unknown): void {
|
|
41
|
+
bridge?.sendChatMessage?.(input, legacyContext)
|
|
42
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { parse, stringify } from 'devalue'
|
|
2
|
+
|
|
3
|
+
import { APPLET_API_BASE_SENTINEL } from './base'
|
|
4
|
+
|
|
5
|
+
const BASE = APPLET_API_BASE_SENTINEL
|
|
6
|
+
|
|
7
|
+
export function rpc(module: string, name: string) {
|
|
8
|
+
return async (...args: unknown[]): Promise<unknown> => {
|
|
9
|
+
const res = await fetch(BASE + '/rpc/' + module + '/' + name, {
|
|
10
|
+
method: 'POST',
|
|
11
|
+
headers: { 'Content-Type': 'application/json' },
|
|
12
|
+
body: stringify(args)
|
|
13
|
+
})
|
|
14
|
+
if (!res.ok) throw new Error(await res.text())
|
|
15
|
+
return parse(await res.text())
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { realpath, rm } from 'node:fs/promises'
|
|
2
|
+
import { join, relative, sep } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { analyzeAppletDependencies, collectDependencies, MODULE_FILE_RE } from './dependencies'
|
|
5
|
+
|
|
6
|
+
type ViewSource = {
|
|
7
|
+
path: string
|
|
8
|
+
source: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function readViewSource(
|
|
12
|
+
workspacePath: string,
|
|
13
|
+
viewId: string
|
|
14
|
+
): Promise<ViewSource | null> {
|
|
15
|
+
const directory = join(workspacePath, '.moi', 'views')
|
|
16
|
+
for (const extension of ['.tsx', '.ts']) {
|
|
17
|
+
const path = join(directory, `${viewId}${extension}`)
|
|
18
|
+
const file = Bun.file(path)
|
|
19
|
+
if (await file.exists()) return { path, source: await file.text() }
|
|
20
|
+
}
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class ViewSourceInUseError extends Error {}
|
|
25
|
+
|
|
26
|
+
function isCleanupSource(moiRoot: string, path: string): boolean {
|
|
27
|
+
if (!path.startsWith(moiRoot + sep) || !MODULE_FILE_RE.test(path)) return false
|
|
28
|
+
// Keep data, installed UI components, packages, and generated/hidden files.
|
|
29
|
+
return !relative(moiRoot, path)
|
|
30
|
+
.split(sep)
|
|
31
|
+
.some(part => part.startsWith('.') || ['data', 'ui', 'node_modules'].includes(part))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function deleteViewSourceFiles(workspacePath: string, viewId: string): Promise<void> {
|
|
35
|
+
const workspaceRoot = await realpath(workspacePath)
|
|
36
|
+
const moiRoot = join(workspaceRoot, '.moi')
|
|
37
|
+
const directory = join(moiRoot, 'views')
|
|
38
|
+
const sources = ['tsx', 'ts'].map(extension => join(directory, `${viewId}.${extension}`))
|
|
39
|
+
const roots = new Set([...sources, join(directory, `${viewId}.server.ts`)])
|
|
40
|
+
const remove = new Set(sources)
|
|
41
|
+
const graph = await analyzeAppletDependencies(workspaceRoot).catch(() => null)
|
|
42
|
+
|
|
43
|
+
if (graph?.complete) {
|
|
44
|
+
// A symlink can give one module several identities. Leave helpers behind
|
|
45
|
+
// in that case rather than guessing ownership or doing a more costly scan.
|
|
46
|
+
const canonical = await Promise.all(
|
|
47
|
+
[...graph.modules.keys()].map(async path => (await realpath(path).catch(() => null)) === path)
|
|
48
|
+
)
|
|
49
|
+
if (canonical.every(Boolean)) {
|
|
50
|
+
const candidates = collectDependencies(graph, roots)
|
|
51
|
+
const retained = collectDependencies(graph, [
|
|
52
|
+
...[...graph.entrypoints].filter(path => !roots.has(path)),
|
|
53
|
+
...[...candidates].filter(path => !isCleanupSource(moiRoot, path))
|
|
54
|
+
])
|
|
55
|
+
if (sources.some(path => retained.has(path))) {
|
|
56
|
+
throw new ViewSourceInUseError(
|
|
57
|
+
'Another applet imports this view. Remove that import before deleting it.'
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
for (const path of candidates) {
|
|
61
|
+
if (!retained.has(path) && isCleanupSource(moiRoot, path)) remove.add(path)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
await Promise.all([...remove].map(path => rm(path, { force: true })))
|
|
67
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { appendAttachments, attachmentPart } from '@/lib/moi-attachments'
|
|
2
|
+
import type { MessageAttachment, Part, Attachment } from '@/lib/types'
|
|
3
|
+
import { materializeToPath, resolveUploads, servedUploadUrl } from './uploads'
|
|
4
|
+
|
|
5
|
+
// Native images carry bytes; every file has a readable path.
|
|
6
|
+
type PreparedAttachment =
|
|
7
|
+
| Exclude<Attachment, { type: 'image' }>
|
|
8
|
+
| (Extract<Attachment, { type: 'image' }> & { data: Buffer })
|
|
9
|
+
|
|
10
|
+
export type PreparedAttachmentMessage = {
|
|
11
|
+
text: string
|
|
12
|
+
attachments: PreparedAttachment[]
|
|
13
|
+
parts: Part[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class AttachmentUploadError extends Error {
|
|
17
|
+
constructor() {
|
|
18
|
+
super('Attachment not found or expired')
|
|
19
|
+
this.name = 'AttachmentUploadError'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveAttachmentUploads(workspaceId: string, inputs: readonly MessageAttachment[]) {
|
|
24
|
+
const ids = inputs.flatMap(input => (input.type === 'upload' ? [input.uploadId] : []))
|
|
25
|
+
const uploads = resolveUploads(workspaceId, ids)
|
|
26
|
+
if (uploads.length !== ids.length) throw new AttachmentUploadError()
|
|
27
|
+
return new Map(uploads.map(upload => [upload.id, upload]))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Path-only harnesses await this before building their message.
|
|
31
|
+
export async function materializeAttachmentPaths(
|
|
32
|
+
workspaceId: string,
|
|
33
|
+
inputs: readonly MessageAttachment[] = []
|
|
34
|
+
): Promise<void> {
|
|
35
|
+
await Promise.all(
|
|
36
|
+
[...resolveAttachmentUploads(workspaceId, inputs).values()].map(materializeToPath)
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Keep construction synchronous so Claude queues a send before an immediate Stop.
|
|
41
|
+
// For path delivery, await materializeAttachmentPaths first.
|
|
42
|
+
export function prepareAttachmentMessage(
|
|
43
|
+
workspaceId: string,
|
|
44
|
+
text: string,
|
|
45
|
+
inputs: readonly MessageAttachment[] = [],
|
|
46
|
+
inlineImages = true
|
|
47
|
+
): PreparedAttachmentMessage {
|
|
48
|
+
const uploads = resolveAttachmentUploads(workspaceId, inputs)
|
|
49
|
+
const attachments: PreparedAttachment[] = []
|
|
50
|
+
const parts: Part[] = []
|
|
51
|
+
for (const input of inputs) {
|
|
52
|
+
if (input.type === 'text') {
|
|
53
|
+
attachments.push(input)
|
|
54
|
+
parts.push(attachmentPart(input))
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
const upload = uploads.get(input.uploadId)
|
|
58
|
+
if (!upload) throw new AttachmentUploadError()
|
|
59
|
+
const metadata = {
|
|
60
|
+
mediaType: upload.mediaType,
|
|
61
|
+
source: input.source
|
|
62
|
+
}
|
|
63
|
+
if (upload.kind === 'image' && inlineImages && upload.data) {
|
|
64
|
+
const attachment: PreparedAttachment = {
|
|
65
|
+
type: 'image',
|
|
66
|
+
...metadata,
|
|
67
|
+
label: upload.filename,
|
|
68
|
+
purpose: input.purpose,
|
|
69
|
+
data: upload.data
|
|
70
|
+
}
|
|
71
|
+
attachments.push(attachment)
|
|
72
|
+
parts.push(attachmentPart(attachment, servedUploadUrl(upload)))
|
|
73
|
+
} else if (upload.path) {
|
|
74
|
+
const attachment: Attachment = {
|
|
75
|
+
type: 'file',
|
|
76
|
+
...metadata,
|
|
77
|
+
path: upload.path,
|
|
78
|
+
...(upload.kind === 'image' ? { purpose: input.purpose } : {})
|
|
79
|
+
}
|
|
80
|
+
attachments.push(attachment)
|
|
81
|
+
parts.push(attachmentPart(attachment))
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const descriptors = attachments.map(attachment => {
|
|
85
|
+
if (!('data' in attachment)) return attachment
|
|
86
|
+
const { data: _data, ...descriptor } = attachment
|
|
87
|
+
return descriptor
|
|
88
|
+
})
|
|
89
|
+
if (text) parts.push({ type: 'text', text })
|
|
90
|
+
return {
|
|
91
|
+
text: appendAttachments(text, descriptors),
|
|
92
|
+
attachments,
|
|
93
|
+
parts
|
|
94
|
+
}
|
|
95
|
+
}
|