@deepagents/elements 7.0.1 → 7.1.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fragment.d.ts","sourceRoot":"","sources":["../../src/context/fragment.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAG3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,iBAAiB,EAAE,GAC/B,eAAe,
|
|
1
|
+
{"version":3,"file":"fragment.d.ts","sourceRoot":"","sources":["../../src/context/fragment.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAG3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,iBAAiB,EAAE,GAC/B,eAAe,CAuBjB"}
|
package/dist/context/index.js
CHANGED
|
@@ -23,10 +23,7 @@ function elementsFragment(descriptors) {
|
|
|
23
23
|
}));
|
|
24
24
|
return {
|
|
25
25
|
name: "elements",
|
|
26
|
-
data: [
|
|
27
|
-
{ name: "instructions", data: ELEMENTS_INSTRUCTIONS },
|
|
28
|
-
...items
|
|
29
|
-
],
|
|
26
|
+
data: [{ name: "instructions", data: ELEMENTS_INSTRUCTIONS }, ...items],
|
|
30
27
|
metadata: { elements: snapshot }
|
|
31
28
|
};
|
|
32
29
|
}
|
|
@@ -101,8 +98,10 @@ function elementChunking() {
|
|
|
101
98
|
return null;
|
|
102
99
|
};
|
|
103
100
|
}
|
|
104
|
-
var
|
|
105
|
-
|
|
101
|
+
var chunking = elementChunking();
|
|
102
|
+
function elementsStreamTransform(options) {
|
|
103
|
+
return smoothStream({ chunking })({ tools: options.tools });
|
|
104
|
+
}
|
|
106
105
|
export {
|
|
107
106
|
elementChunking,
|
|
108
107
|
elementsFragment,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/context/fragment.ts", "../../src/lib/serialize.ts", "../../src/context/transform.ts"],
|
|
4
|
-
"sourcesContent": ["import dedent from 'dedent';\n\nimport type { ContextFragment } from '@deepagents/context';\n\nimport { toDescriptor } from '../lib/serialize.ts';\nimport type { ElementDescriptor } from '../lib/types.ts';\n\n/**\n * Create a context fragment advertising the interactive-element catalog to\n * the model.\n *\n * Built from structured child fragments (like the skills fragment) rather\n * than a pre-formatted XML string: the XmlRenderer HTML-escapes string data,\n * so a pre-rendered blob would reach the model as `<element>` entities\n * instead of real tags.\n *\n * The descriptor snapshot rides on `metadata` for programmatic readback; it\n * is not rendered.\n *\n * @example\n * ```ts\n * context.set(elementsFragment([\n * { name: 'followup', allowedAttributes: ['question'] },\n * ]));\n * ```\n */\nexport function elementsFragment(\n descriptors: ElementDescriptor[],\n): ContextFragment {\n const snapshot = descriptors.map(toDescriptor);\n\n if (snapshot.length === 0) {\n return { name: 'elements', data: [], metadata: { elements: snapshot } };\n }\n\n const items: ContextFragment[] = snapshot.map((descriptor) => ({\n name: 'element',\n data: {\n name: descriptor.name,\n ...(descriptor.description\n ? { description: descriptor.description }\n : {}),\n 'allowed-attributes': descriptor.allowedAttributes.join(', '),\n },\n }));\n\n return {\n name: 'elements',\n data: [
|
|
5
|
-
"mappings": ";AAAA,OAAO,YAAY;;;ACEZ,SAAS,aACd,SACmB;AACnB,QAAM,EAAE,MAAM,mBAAmB,YAAY,IAAI;AACjD,SAAO,EAAE,MAAM,mBAAmB,YAAY;AAChD;;;ADmBO,SAAS,iBACd,aACiB;AACjB,QAAM,WAAW,YAAY,IAAI,YAAY;AAE7C,MAAI,SAAS,WAAW,GAAG;AACzB,WAAO,EAAE,MAAM,YAAY,MAAM,CAAC,GAAG,UAAU,EAAE,UAAU,SAAS,EAAE;AAAA,EACxE;AAEA,QAAM,QAA2B,SAAS,IAAI,CAAC,gBAAgB;AAAA,IAC7D,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,MAAM,WAAW;AAAA,MACjB,GAAI,WAAW,cACX,EAAE,aAAa,WAAW,YAAY,IACtC,CAAC;AAAA,MACL,sBAAsB,WAAW,kBAAkB,KAAK,IAAI;AAAA,IAC9D;AAAA,EACF,EAAE;AAEF,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM
|
|
4
|
+
"sourcesContent": ["import dedent from 'dedent';\n\nimport type { ContextFragment } from '@deepagents/context';\n\nimport { toDescriptor } from '../lib/serialize.ts';\nimport type { ElementDescriptor } from '../lib/types.ts';\n\n/**\n * Create a context fragment advertising the interactive-element catalog to\n * the model.\n *\n * Built from structured child fragments (like the skills fragment) rather\n * than a pre-formatted XML string: the XmlRenderer HTML-escapes string data,\n * so a pre-rendered blob would reach the model as `<element>` entities\n * instead of real tags.\n *\n * The descriptor snapshot rides on `metadata` for programmatic readback; it\n * is not rendered.\n *\n * @example\n * ```ts\n * context.set(elementsFragment([\n * { name: 'followup', allowedAttributes: ['question'] },\n * ]));\n * ```\n */\nexport function elementsFragment(\n descriptors: ElementDescriptor[],\n): ContextFragment {\n const snapshot = descriptors.map(toDescriptor);\n\n if (snapshot.length === 0) {\n return { name: 'elements', data: [], metadata: { elements: snapshot } };\n }\n\n const items: ContextFragment[] = snapshot.map((descriptor) => ({\n name: 'element',\n data: {\n name: descriptor.name,\n ...(descriptor.description\n ? { description: descriptor.description }\n : {}),\n 'allowed-attributes': descriptor.allowedAttributes.join(', '),\n },\n }));\n\n return {\n name: 'elements',\n data: [{ name: 'instructions', data: ELEMENTS_INSTRUCTIONS }, ...items],\n metadata: { elements: snapshot },\n };\n}\n\nconst ELEMENTS_INSTRUCTIONS = dedent`The UI elements listed below can be rendered inline in your response: write one as an HTML-style tag directly in your prose (e.g. \\`<element-name attribute=\"value\" />\\`) and the client renders it as an interactive component.\n\n### How to use elements\n- Use an element only when the conversation matches its declared purpose.\n- Only use element names from this list. Never invent elements.\n- Only write attributes listed in that element's allowed-attributes. Never invent attributes.\n- Keep the rest of your response as normal markdown; elements flow inline with the text.\n`;\n", "import { type ElementDescriptor, findDuplicateName } from './types.ts';\n\nexport function toDescriptor<T extends ElementDescriptor>(\n element: T,\n): ElementDescriptor {\n const { name, allowedAttributes, description } = element;\n return { name, allowedAttributes, description };\n}\n\n/**\n * Base-wins merging: an extra whose name collides with a base element is\n * dropped, so a platform's defaults cannot be hijacked by app extras.\n * Duplicate names WITHIN extras throw \u2014 silently keeping either copy would\n * make the prompt catalog and the render registry disagree.\n */\nexport function mergeElements<T extends ElementDescriptor>(\n base: T[],\n extras: T[] | undefined,\n): T[] {\n if (!extras || extras.length === 0) return base;\n const duplicate = findDuplicateName(extras);\n if (duplicate !== undefined) {\n throw new Error(\n `Duplicate extra element <${duplicate}>: a catalog must map each tag to exactly one element.`,\n );\n }\n const baseNames = new Set(base.map((el) => el.name));\n const additions = extras.filter((el) => !baseNames.has(el.name));\n return [...base, ...additions];\n}\n\nfunction escapeXmlAttribute(value: string): string {\n return value\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"');\n}\n\nexport function formatElementsCatalog(elements: ElementDescriptor[]): string {\n if (elements.length === 0) return '';\n\n const items = elements\n .map((el) => {\n const description = el.description\n ? ` description=\"${escapeXmlAttribute(el.description)}\"`\n : '';\n const attrs = el.allowedAttributes.map(escapeXmlAttribute).join(', ');\n return `<element name=\"${escapeXmlAttribute(el.name)}\"${description}>\\n <allowed-attributes>${attrs}</allowed-attributes>\\n</element>`;\n })\n .join('\\n');\n\n return `<elements>\nThe following UI components are available to render inline in your response.\nUse them only when the user's question matches their declared purpose. Never\ninvent attributes outside the listed allowedAttributes for each element.\n\n${items}\n</elements>`;\n}\n", "import { type StreamTextTransform, type ToolSet, smoothStream } from 'ai';\nimport { Parser } from 'htmlparser2';\n\n/**\n * A `smoothStream` chunking detector that buffers an HTML-style element\n * until its root tag closes, so no partial custom tag ever reaches the\n * client mid-stream.\n *\n * - Regular text: chunks word-by-word (same as smoothStream's default)\n * - Elements: held until complete, including self-closing (`<kpi />`)\n * - Closing tags inside HTML comments are ignored\n * - Content passes through verbatim: `smoothStream` requires every detected\n * chunk to be a prefix of its buffer, so rewriting (e.g. unescaping a\n * literal `\\n` in an attribute) belongs to the rendering client\n *\n * Boundary control only applies while consecutive text deltas stream:\n * `smoothStream` force-flushes its buffer when a non-text chunk arrives, so\n * an element left unclosed at the end of a text part is emitted raw.\n */\nexport function elementChunking(): (buffer: string) => string | null {\n const WORD_REGEX = /\\S+\\s+/m;\n\n return (buffer: string): string | null => {\n if (!buffer.startsWith('<')) {\n const ltIndex = buffer.indexOf('<');\n if (ltIndex > 0) {\n const textBefore = buffer.slice(0, ltIndex);\n const wordMatch = WORD_REGEX.exec(textBefore);\n if (wordMatch) {\n return textBefore.slice(0, wordMatch.index + wordMatch[0].length);\n }\n return textBefore;\n }\n const wordMatch = WORD_REGEX.exec(buffer);\n if (wordMatch) {\n return buffer.slice(0, wordMatch.index + wordMatch[0].length);\n }\n return null;\n }\n\n // A `<` not opening a tag (e.g. `< ` or `<123`) is plain text\n if (!/^<[a-z]/i.test(buffer)) {\n const wordMatch = WORD_REGEX.exec(buffer);\n if (wordMatch) {\n return buffer.slice(0, wordMatch.index + wordMatch[0].length);\n }\n return null;\n }\n\n // `<bar` could still become `<bar-chart`; wait for the name to finish\n if (/^<[a-z][a-z0-9-]*$/i.test(buffer)) {\n return null;\n }\n\n let elementEnd = -1;\n let depth = 0;\n let rootTagName: string | null = null;\n\n const parser = new Parser(\n {\n onopentag(name) {\n if (rootTagName === null) {\n rootTagName = name;\n }\n depth++;\n },\n onclosetag(name) {\n depth--;\n // First completed root element wins: without the guard, a later\n // same-name sibling in the buffer would keep pushing elementEnd\n // forward, merging separate elements (and any text between them)\n // into one chunk\n if (elementEnd === -1 && depth === 0 && name === rootTagName) {\n elementEnd = parser.endIndex + 1;\n }\n },\n },\n {\n recognizeSelfClosing: true,\n lowerCaseTags: true,\n },\n );\n\n // No parser.end(): it would auto-close a dangling tag and report a\n // still-incomplete element as finished\n parser.write(buffer);\n\n if (elementEnd !== -1) {\n return buffer.slice(0, elementEnd);\n }\n\n return null;\n };\n}\n\nconst chunking = elementChunking();\n\n/**\n * Element-safe replacement for the default `smoothStream()` transform. Pass\n * it to `chat()`/`agent.stream()` whenever an element catalog is active:\n *\n * ```ts\n * const stream = await chat(agent, { transform: [elementsStreamTransform] });\n * ```\n *\n * Generic over the agent's tool set, so it fits any `StreamTextTransform<TOOLS>`\n * (smoothStream fixes its tool set when called, not per stream).\n */\nexport function elementsStreamTransform<TOOLS extends ToolSet>(\n options: Parameters<StreamTextTransform<TOOLS>>[0],\n): ReturnType<StreamTextTransform<TOOLS>> {\n return smoothStream<TOOLS>({ chunking })({ tools: options.tools });\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAO,YAAY;;;ACEZ,SAAS,aACd,SACmB;AACnB,QAAM,EAAE,MAAM,mBAAmB,YAAY,IAAI;AACjD,SAAO,EAAE,MAAM,mBAAmB,YAAY;AAChD;;;ADmBO,SAAS,iBACd,aACiB;AACjB,QAAM,WAAW,YAAY,IAAI,YAAY;AAE7C,MAAI,SAAS,WAAW,GAAG;AACzB,WAAO,EAAE,MAAM,YAAY,MAAM,CAAC,GAAG,UAAU,EAAE,UAAU,SAAS,EAAE;AAAA,EACxE;AAEA,QAAM,QAA2B,SAAS,IAAI,CAAC,gBAAgB;AAAA,IAC7D,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,MAAM,WAAW;AAAA,MACjB,GAAI,WAAW,cACX,EAAE,aAAa,WAAW,YAAY,IACtC,CAAC;AAAA,MACL,sBAAsB,WAAW,kBAAkB,KAAK,IAAI;AAAA,IAC9D;AAAA,EACF,EAAE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ACF;AAAA,QACA,WAAW,MAAM;AACf;AAKA,cAAI,eAAe,MAAM,UAAU,KAAK,SAAS,aAAa;AAC5D,yBAAa,OAAO,WAAW;AAAA,UACjC;AAAA,QACF;AAAA,MACF;AAAA,MACA;AAAA,QACE,sBAAsB;AAAA,QACtB,eAAe;AAAA,MACjB;AAAA,IACF;AAIA,WAAO,MAAM,MAAM;AAEnB,QAAI,eAAe,IAAI;AACrB,aAAO,OAAO,MAAM,GAAG,UAAU;AAAA,IACnC;AAEA,WAAO;AAAA,EACT;AACF;AAEA,IAAM,WAAW,gBAAgB;AAa1B,SAAS,wBACd,SACwC;AACxC,SAAO,aAAoB,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,QAAQ,MAAM,CAAC;AACnE;",
|
|
6
6
|
"names": ["wordMatch"]
|
|
7
7
|
}
|
|
@@ -23,6 +23,9 @@ export declare function elementChunking(): (buffer: string) => string | null;
|
|
|
23
23
|
* ```ts
|
|
24
24
|
* const stream = await chat(agent, { transform: [elementsStreamTransform] });
|
|
25
25
|
* ```
|
|
26
|
+
*
|
|
27
|
+
* Generic over the agent's tool set, so it fits any `StreamTextTransform<TOOLS>`
|
|
28
|
+
* (smoothStream fixes its tool set when called, not per stream).
|
|
26
29
|
*/
|
|
27
|
-
export declare
|
|
30
|
+
export declare function elementsStreamTransform<TOOLS extends ToolSet>(options: Parameters<StreamTextTransform<TOOLS>>[0]): ReturnType<StreamTextTransform<TOOLS>>;
|
|
28
31
|
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/context/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,KAAK,OAAO,EAAgB,MAAM,IAAI,CAAC;AAG1E;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,IAAI,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CA0EnE;
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/context/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,KAAK,OAAO,EAAgB,MAAM,IAAI,CAAC;AAG1E;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,IAAI,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CA0EnE;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,SAAS,OAAO,EAC3D,OAAO,EAAE,UAAU,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GACjD,UAAU,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAExC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepagents/elements",
|
|
3
|
-
"version": "7.0
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,22 +12,6 @@
|
|
|
12
12
|
},
|
|
13
13
|
"homepage": "https://github.com/JanuaryLabs/deepagents#readme",
|
|
14
14
|
"type": "module",
|
|
15
|
-
"nx": {
|
|
16
|
-
"targets": {
|
|
17
|
-
"test": {
|
|
18
|
-
"inputs": [
|
|
19
|
-
"default",
|
|
20
|
-
"^production"
|
|
21
|
-
],
|
|
22
|
-
"outputs": [
|
|
23
|
-
"{workspaceRoot}/test-results/elements.xml"
|
|
24
|
-
],
|
|
25
|
-
"options": {
|
|
26
|
-
"command": "node ../../tools/src/run-node-tests.ts ../../test-results/elements.xml"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
15
|
"main": "./dist/index.js",
|
|
32
16
|
"module": "./dist/index.js",
|
|
33
17
|
"types": "./dist/index.d.ts",
|
|
@@ -54,13 +38,13 @@
|
|
|
54
38
|
"access": "public"
|
|
55
39
|
},
|
|
56
40
|
"dependencies": {
|
|
57
|
-
"ai": "^7.0.
|
|
41
|
+
"ai": "^7.0.127",
|
|
58
42
|
"dedent": "^1.7.2",
|
|
59
43
|
"htmlparser2": "^10.0.0",
|
|
60
44
|
"zod": "^4.4.3"
|
|
61
45
|
},
|
|
62
46
|
"peerDependencies": {
|
|
63
|
-
"@deepagents/context": "7.0
|
|
47
|
+
"@deepagents/context": "7.1.0"
|
|
64
48
|
},
|
|
65
49
|
"peerDependenciesMeta": {
|
|
66
50
|
"@deepagents/context": {
|
|
@@ -68,6 +52,6 @@
|
|
|
68
52
|
}
|
|
69
53
|
},
|
|
70
54
|
"devDependencies": {
|
|
71
|
-
"@deepagents/context": "7.0
|
|
55
|
+
"@deepagents/context": "7.1.0"
|
|
72
56
|
}
|
|
73
57
|
}
|