@ai-gui/core 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -0
- package/dist/index.cjs +1647 -41
- package/dist/index.d.cts +434 -8
- package/dist/index.d.ts +434 -8
- package/dist/index.js +1610 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @ai-gui/core
|
|
2
2
|
|
|
3
|
+
The core also exposes provider-neutral model stream primitives:
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { contentDeltas, mockModelStream, parseSSE } from "@ai-gui/core"
|
|
7
|
+
|
|
8
|
+
const events = mockModelStream([
|
|
9
|
+
{ type: "content", delta: "Hello" },
|
|
10
|
+
{ type: "usage", data: { outputTokens: 1 } },
|
|
11
|
+
])
|
|
12
|
+
await renderer.feed(contentDeltas(events))
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`parseSSE`, `jsonLines`/`ndjson`, and `textLines` accept fetch responses, byte streams, and async byte iterables. They preserve split UTF-8 characters, support cancellation, release readers, and let callers reject or skip malformed records. `readableBytes` and `mockModelStream` are deterministic helpers for tests and examples.
|
|
16
|
+
|
|
3
17
|
The headless streaming engine behind [AIGUI](../../README.md) — framework-agnostic. It parses a streaming LLM response into an AST + patches, runs plugin node renderers, sanitizes HTML, and builds the system prompt. Use it directly, or via an adapter (`@ai-gui/react`, `@ai-gui/vue`, `@ai-gui/vanilla`).
|
|
4
18
|
|
|
5
19
|
## Install
|
|
@@ -32,11 +46,75 @@ await renderer.feed(response.body!)
|
|
|
32
46
|
const system = buildSystemPrompt({ registry })
|
|
33
47
|
```
|
|
34
48
|
|
|
49
|
+
## Actions
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { ActionRegistry, createActionRuntime } from "@ai-gui/core"
|
|
53
|
+
|
|
54
|
+
const actions = new ActionRegistry()
|
|
55
|
+
actions.register<{ city: string }, unknown>({
|
|
56
|
+
type: "weather.refresh",
|
|
57
|
+
schema: {
|
|
58
|
+
type: "object",
|
|
59
|
+
required: ["city"],
|
|
60
|
+
properties: { city: { type: "string" } },
|
|
61
|
+
},
|
|
62
|
+
run: async (params, { signal, actionId, cardType }) => {
|
|
63
|
+
return fetch(`/api/weather?city=${encodeURIComponent(params.city)}`, { signal }).then((r) => r.json())
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const runtime = createActionRuntime({ registry: actions, timeoutMs: 10_000 })
|
|
68
|
+
const result = await runtime.dispatch({
|
|
69
|
+
type: "weather.refresh",
|
|
70
|
+
params: { city: "Tokyo" },
|
|
71
|
+
cardType: "weather",
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Use `runtime.subscribe()`, `runtime.getState(key)`, `runtime.cancel(key)`, `runtime.reset()` and `runtime.destroy()` to observe and control actions. Automatic dispatch errors are observed through runtime state or adapter hooks; `onCardAction` / `card-action` only observe action events. Pending duplicate dispatches from the same owner share one Promise; adapters provide isolated owners automatically.
|
|
76
|
+
|
|
77
|
+
## Stateful cards
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { ActionRegistry, CardStore, createActionRuntime } from "@ai-gui/core"
|
|
81
|
+
|
|
82
|
+
const cardStore = new CardStore({ registry })
|
|
83
|
+
cardStore.register({
|
|
84
|
+
id: "weather-tokyo",
|
|
85
|
+
type: "weather",
|
|
86
|
+
data: { id: "weather-tokyo", city: "Tokyo", tempC: 24 },
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
cardStore.apply({
|
|
90
|
+
op: "merge",
|
|
91
|
+
cardId: "weather-tokyo",
|
|
92
|
+
data: { tempC: 25 },
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const snapshot = cardStore.snapshot()
|
|
96
|
+
cardStore.restore(JSON.parse(JSON.stringify(snapshot)))
|
|
97
|
+
|
|
98
|
+
const actions = new ActionRegistry()
|
|
99
|
+
actions.register({
|
|
100
|
+
type: "weather.refresh",
|
|
101
|
+
async run(_params, { cardId }) {
|
|
102
|
+
return { op: "merge", cardId: cardId!, data: { tempC: 26 } }
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const runtime = createActionRuntime({ registry: actions, cardStore })
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`CardStore` supports initialize-if-absent registration, immutable records, recursive object merge, replace, atomic patch batches, revision checks, subscriptions, delete/clear, and snapshot/restore. Action patch results use optimistic mutation epochs, so an older Action cannot overwrite a Card changed, deleted, recreated, or restored after that Action started.
|
|
110
|
+
|
|
35
111
|
## Exports
|
|
36
112
|
|
|
37
113
|
- `Renderer` — `push(chunk)`, `feed(AsyncIterable | ReadableStream)`, `reset()`; constructor `{ registry?, plugins?, sanitize?, onPatch?(patches, nodes) }`.
|
|
38
114
|
- `StreamRouter` — demultiplex one stream into named channels: `.channel(name, sink)`, `.on(name, cb)`, `.feed(source)`.
|
|
39
115
|
- `CardRegistry` — `register(def)`, `parse(type, rawJson)`, `getRender(type)`, `toPromptSpec()`, `toJSONSchema()`.
|
|
116
|
+
- `CardStore` — `register`, `get`, `list`, `subscribe`, `apply`, `applyAll`, `delete`, `clear`, `snapshot`, and `restore` for Cards with stable IDs.
|
|
117
|
+
- `ActionRegistry`, `ActionRuntime`, `createActionRuntime`, `getActionKey`, `getIdleActionState` — validated application-owned action execution and observable lifecycle state.
|
|
40
118
|
- `buildSystemPrompt({ base?, registry?, plugins? })`.
|
|
41
119
|
- Utilities: `parsePartialJSON`, `repairMarkdown`, `sanitizeHtml`, `createParser`, `diffAst`, `collectNodeRenderers`.
|
|
42
120
|
- Types: `ASTNode`, `Patch`, `RenderOutput` (`html | element | card | mount`), `CardDef`, `AIGuiPlugin`, `NodeRenderer`, `RendererOptions`, `JSONSchema`.
|