源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s

This commit is contained in:
agent-radar committed 2026-09-01 20:17:27 +08:00
commit 2ed3415fb7
8018 files changed
+1936476

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
# @tutti-os/ui-react-hooks
Host-agnostic React hook helpers for shared Tutti frontend packages.
This package is published to npm as `@tutti-os/ui-react-hooks`.
Use it for narrow reusable React hook patterns such as external-store snapshot
subscription, selector reads, and IME-safe controlled input state. Keep domain
hooks and UI components in their owning packages.
Prefer this package over adding new direct `useSyncExternalStore` wrappers in
shared frontend packages so subscription semantics stay consistent.
`useExternalStoreSnapshot` keeps its React subscription callbacks stable while
the source identity is unchanged, so source consumers do not unsubscribe and
resubscribe on each render.
This package does not replace adapter-level snapshot memoization. If a non-React
adapter exposes a derived `getSnapshot()`, keep reference-stable snapshot reuse
in the owning package.
+44
View File
@@ -0,0 +1,44 @@
{
"name": "@tutti-os/ui-react-hooks",
"version": "0.1.0",
"private": false,
"type": "module",
"types": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"files": [
"dist",
"README.md"
],
"repository": {
"type": "git",
"url": "git+https://github.com/tutti-os/tutti.git",
"directory": "packages/ui/react-hooks"
},
"scripts": {
"build": "tsup --config tsup.config.ts",
"test": "node --test --experimental-strip-types \"./src/**/*.test.ts\"",
"typecheck": "node ../../../tools/scripts/run-tsgo-typecheck.mjs"
},
"devDependencies": {
"@tutti-os/config-tsconfig": "workspace:*",
"@types/node": "^24.0.1",
"@types/react": "^19.1.6",
"react": "^19.1.0",
"typescript": "^5.8.3"
},
"peerDependencies": {
"react": "^19.1.0"
},
"publishConfig": {
"access": "public",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"types": "./dist/index.d.ts"
}
}
+14
View File
@@ -0,0 +1,14 @@
export type { ExternalStoreSnapshotSource } from "./types.ts";
export type {
ComposedInputPendingCommit,
ComposedInputValueSyncInput,
ComposedInputValueSyncResult,
UseComposedInputValueInput,
UseComposedInputValueResult
} from "./useComposedInputValue.ts";
export {
resolveComposedInputValueSync,
useComposedInputValue
} from "./useComposedInputValue.ts";
export { useExternalStoreSelector } from "./useExternalStoreSelector.ts";
export { useExternalStoreSnapshot } from "./useExternalStoreSnapshot.ts";
+5
View File
@@ -0,0 +1,5 @@
export interface ExternalStoreSnapshotSource<TSnapshot> {
getServerSnapshot?(): TSnapshot;
getSnapshot(): TSnapshot;
subscribe(listener: () => void): () => void;
}
@@ -0,0 +1,117 @@
import assert from "node:assert/strict";
import test from "node:test";
import { resolveComposedInputValueSync } from "./useComposedInputValue.ts";
test("composed input sync keeps the local value while IME composition is active", () => {
assert.deepEqual(
resolveComposedInputValueSync({
isComposing: true,
localValue: "ab中",
pendingCommit: null,
value: "ab"
}),
{
pendingCommit: null,
shouldSyncLocalValue: false,
value: "ab中"
}
);
});
test("composed input sync ignores stale parent values after a local IME commit", () => {
assert.deepEqual(
resolveComposedInputValueSync({
isComposing: false,
localValue: "ab中文cd",
pendingCommit: {
committedValue: "ab中文cd",
previousValue: "abcd"
},
value: "abcd"
}),
{
pendingCommit: {
committedValue: "ab中文cd",
previousValue: "abcd"
},
shouldSyncLocalValue: false,
value: "ab中文cd"
}
);
});
test("composed input sync clears a pending commit after the parent catches up", () => {
assert.deepEqual(
resolveComposedInputValueSync({
isComposing: false,
localValue: "ab中文cd",
pendingCommit: {
committedValue: "ab中文cd",
previousValue: "abcd"
},
value: "ab中文cd"
}),
{
pendingCommit: null,
shouldSyncLocalValue: false,
value: "ab中文cd"
}
);
});
test("composed input sync keeps a later local commit while the parent is still stale", () => {
assert.deepEqual(
resolveComposedInputValueSync({
isComposing: false,
localValue: "ab中文cde",
pendingCommit: {
committedValue: "ab中文cde",
previousValue: "abcd"
},
value: "abcd"
}),
{
pendingCommit: {
committedValue: "ab中文cde",
previousValue: "abcd"
},
shouldSyncLocalValue: false,
value: "ab中文cde"
}
);
});
test("composed input sync still accepts external value changes without a pending commit", () => {
assert.deepEqual(
resolveComposedInputValueSync({
isComposing: false,
localValue: "Draft",
pendingCommit: null,
value: ""
}),
{
pendingCommit: null,
shouldSyncLocalValue: true,
value: ""
}
);
});
test("composed input sync accepts external changes after a local commit", () => {
assert.deepEqual(
resolveComposedInputValueSync({
isComposing: false,
localValue: "ab中文cd",
pendingCommit: {
committedValue: "ab中文cd",
previousValue: "abcd"
},
value: "external reset"
}),
{
pendingCommit: null,
shouldSyncLocalValue: true,
value: "external reset"
}
);
});
@@ -0,0 +1,143 @@
import {
useEffect,
useRef,
useState,
type ChangeEvent,
type CompositionEvent,
type FocusEvent
} from "react";
export interface ComposedInputPendingCommit {
committedValue: string;
previousValue: string;
}
export interface ComposedInputValueSyncInput {
isComposing: boolean;
localValue: string;
pendingCommit: ComposedInputPendingCommit | null;
value: string;
}
export interface ComposedInputValueSyncResult {
pendingCommit: ComposedInputPendingCommit | null;
shouldSyncLocalValue: boolean;
value: string;
}
export interface UseComposedInputValueInput {
onCommit: (value: string) => void;
value: string;
}
export interface UseComposedInputValueResult {
clearValue: () => void;
commitValue: (value: string) => void;
isComposing: boolean;
onBlur: (event: FocusEvent<HTMLInputElement>) => void;
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
onCompositionEnd: (event: CompositionEvent<HTMLInputElement>) => void;
onCompositionStart: () => void;
value: string;
}
export function resolveComposedInputValueSync(
input: ComposedInputValueSyncInput
): ComposedInputValueSyncResult {
if (input.isComposing) {
return {
pendingCommit: input.pendingCommit,
shouldSyncLocalValue: false,
value: input.localValue
};
}
if (input.pendingCommit !== null) {
if (input.value === input.pendingCommit.committedValue) {
return {
pendingCommit: null,
shouldSyncLocalValue: input.localValue !== input.value,
value: input.value
};
}
if (
input.localValue === input.pendingCommit.committedValue &&
input.value === input.pendingCommit.previousValue
) {
return {
pendingCommit: input.pendingCommit,
shouldSyncLocalValue: false,
value: input.localValue
};
}
}
return {
pendingCommit: null,
shouldSyncLocalValue: input.localValue !== input.value,
value: input.value
};
}
export function useComposedInputValue({
onCommit,
value
}: UseComposedInputValueInput): UseComposedInputValueResult {
const pendingCommitRef = useRef<ComposedInputPendingCommit | null>(null);
const isComposingRef = useRef(false);
const [localValue, setLocalValue] = useState(value);
const [isComposing, setIsComposing] = useState(false);
useEffect(() => {
const syncResult = resolveComposedInputValueSync({
isComposing,
localValue,
pendingCommit: pendingCommitRef.current,
value
});
pendingCommitRef.current = syncResult.pendingCommit;
if (syncResult.shouldSyncLocalValue) {
setLocalValue(syncResult.value);
}
}, [isComposing, localValue, value]);
const commitValue = (nextValue: string) => {
pendingCommitRef.current = {
committedValue: nextValue,
previousValue: value
};
setLocalValue(nextValue);
onCommit(nextValue);
};
return {
clearValue: () => {
commitValue("");
},
commitValue,
isComposing,
onBlur: (event: FocusEvent<HTMLInputElement>) => {
commitValue(event.currentTarget.value);
},
onChange: (event: ChangeEvent<HTMLInputElement>) => {
const nextValue = event.currentTarget.value;
if (!isComposingRef.current) {
commitValue(nextValue);
return;
}
setLocalValue(nextValue);
},
onCompositionEnd: (event: CompositionEvent<HTMLInputElement>) => {
isComposingRef.current = false;
setIsComposing(false);
commitValue(event.currentTarget.value);
},
onCompositionStart: () => {
isComposingRef.current = true;
setIsComposing(true);
},
value: localValue
};
}
@@ -0,0 +1,33 @@
import { useRef, useSyncExternalStore } from "react";
import type { ExternalStoreSnapshotSource } from "./types.ts";
export function useExternalStoreSelector<TSnapshot, TResult>(
source: ExternalStoreSnapshotSource<TSnapshot>,
selector: (snapshot: TSnapshot) => TResult
): TResult {
const selectedSnapshotRef = useRef<{
selector: (snapshot: TSnapshot) => TResult;
snapshot: TSnapshot;
value: TResult;
} | null>(null);
const readSelection = (snapshot: TSnapshot) => {
const selectedSnapshot = selectedSnapshotRef.current;
if (
selectedSnapshot?.snapshot === snapshot &&
selectedSnapshot.selector === selector
) {
return selectedSnapshot.value;
}
const value = selector(snapshot);
selectedSnapshotRef.current = { selector, snapshot, value };
return value;
};
return useSyncExternalStore(
(listener) => source.subscribe(listener),
() => readSelection(source.getSnapshot()),
() => readSelection((source.getServerSnapshot ?? source.getSnapshot)())
);
}
@@ -0,0 +1,17 @@
import { useCallback, useSyncExternalStore } from "react";
import type { ExternalStoreSnapshotSource } from "./types.ts";
export function useExternalStoreSnapshot<TSnapshot>(
source: ExternalStoreSnapshotSource<TSnapshot>
): TSnapshot {
const subscribe = useCallback(
(listener: () => void) => source.subscribe(listener),
[source]
);
const getSnapshot = useCallback(() => source.getSnapshot(), [source]);
const getServerSnapshot = useCallback(
() => (source.getServerSnapshot ?? source.getSnapshot)(),
[source]
);
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
}
+9
View File
@@ -0,0 +1,9 @@
{
"extends": "../../configs/tsconfig/base.json",
"compilerOptions": {
"allowImportingTsExtensions": true,
"types": ["node", "react"],
"rootDir": "src"
},
"include": ["src/**/*.ts"]
}
+11
View File
@@ -0,0 +1,11 @@
import { defineConfig } from "tsup";
export default defineConfig({
clean: true,
dts: true,
entry: {
index: "src/index.ts"
},
format: ["esm"],
sourcemap: true
});