@runtypelabs/persona 4.2.0 → 4.3.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.
Files changed (43) hide show
  1. package/README.md +22 -6
  2. package/dist/animations/glyph-cycle.d.cts +1 -1
  3. package/dist/animations/glyph-cycle.d.ts +1 -1
  4. package/dist/animations/{types-B_xbfvR0.d.cts → types-C6tFDxKy.d.cts} +1 -1
  5. package/dist/animations/{types-B_xbfvR0.d.ts → types-C6tFDxKy.d.ts} +1 -1
  6. package/dist/animations/wipe.d.cts +1 -1
  7. package/dist/animations/wipe.d.ts +1 -1
  8. package/dist/codegen.cjs +6 -6
  9. package/dist/codegen.js +9 -9
  10. package/dist/index.cjs +44 -44
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +101 -3
  13. package/dist/index.d.ts +101 -3
  14. package/dist/index.global.js +33 -33
  15. package/dist/index.global.js.map +1 -1
  16. package/dist/index.js +44 -44
  17. package/dist/index.js.map +1 -1
  18. package/dist/install.global.js +1 -1
  19. package/dist/install.global.js.map +1 -1
  20. package/dist/launcher.global.js.map +1 -1
  21. package/dist/smart-dom-reader.d.cts +76 -1
  22. package/dist/smart-dom-reader.d.ts +76 -1
  23. package/dist/theme-editor-preview.cjs +35 -35
  24. package/dist/theme-editor-preview.d.cts +76 -1
  25. package/dist/theme-editor-preview.d.ts +76 -1
  26. package/dist/theme-editor-preview.js +37 -37
  27. package/dist/theme-editor.cjs +1 -1
  28. package/dist/theme-editor.d.cts +76 -1
  29. package/dist/theme-editor.d.ts +76 -1
  30. package/dist/theme-editor.js +1 -1
  31. package/package.json +1 -1
  32. package/src/client.test.ts +349 -29
  33. package/src/client.ts +96 -24
  34. package/src/defaults.ts +2 -0
  35. package/src/index-core.ts +2 -0
  36. package/src/install.ts +9 -1
  37. package/src/session.ts +6 -3
  38. package/src/session.voice.test.ts +65 -0
  39. package/src/types.ts +57 -2
  40. package/src/utils/__fixtures__/unified-translator.oracle.ts +3 -3
  41. package/src/utils/code-generators.ts +10 -0
  42. package/src/utils/target.test.ts +51 -0
  43. package/src/utils/target.ts +90 -0
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Target resolution for the normalized, backend-neutral `target` field.
3
+ *
4
+ * `target` is a single string that selects which backend resource a widget
5
+ * talks to, optimized for a browser widget (always serializable, no live
6
+ * objects). Three shapes are supported:
7
+ *
8
+ * - Runtype TypeID (no prefix): `"agent_…"` / `"flow_…"` route to the
9
+ * Runtype agent/flow paths. The TypeID prefix is self-describing, so no
10
+ * wrapper is needed for the common case.
11
+ * - Provider-prefixed: `"<provider>:<id>"` is handed to the matching
12
+ * `targetProviders[provider]` resolver, which returns the dispatch payload
13
+ * fragment for that backend (e.g. `eve`, `langgraph`). `"runtype:…"` is a
14
+ * built-in that re-detects a TypeID.
15
+ * - Bare name: `"support"` requires a `targetProviders.default` resolver,
16
+ * otherwise it throws (a bare name is ambiguous without one).
17
+ *
18
+ * Resolvers are registered, not passed as the value, which keeps `target`
19
+ * itself a plain string that survives script-tag installs, `data-config`,
20
+ * persisted state, and codegen.
21
+ */
22
+
23
+ /** Resolver for a provider-prefixed (or default) target id. */
24
+ export type TargetResolver = (id: string) => { payload: Record<string, unknown> };
25
+
26
+ /** Normalized routing produced from a `target` string. */
27
+ export type ResolvedTarget =
28
+ | { kind: "agentId"; agentId: string }
29
+ | { kind: "flowId"; flowId: string }
30
+ | { kind: "payload"; payload: Record<string, unknown> };
31
+
32
+ const RUNTYPE_AGENT_PREFIX = "agent_";
33
+ const RUNTYPE_FLOW_PREFIX = "flow_";
34
+
35
+ function detectRuntypeTypeId(id: string): ResolvedTarget | null {
36
+ if (id.startsWith(RUNTYPE_AGENT_PREFIX)) return { kind: "agentId", agentId: id };
37
+ if (id.startsWith(RUNTYPE_FLOW_PREFIX)) return { kind: "flowId", flowId: id };
38
+ return null;
39
+ }
40
+
41
+ /**
42
+ * Resolve a `target` string into normalized routing. Pure and synchronous so
43
+ * it can run on the dispatch hot path and be unit-tested in isolation.
44
+ */
45
+ export function resolveTarget(
46
+ target: string,
47
+ targetProviders?: Record<string, TargetResolver>,
48
+ ): ResolvedTarget {
49
+ const trimmed = target.trim();
50
+ if (!trimmed) {
51
+ throw new Error("[Persona] `target` is empty.");
52
+ }
53
+
54
+ const colon = trimmed.indexOf(":");
55
+ if (colon > 0) {
56
+ const prefix = trimmed.slice(0, colon);
57
+ const rest = trimmed.slice(colon + 1);
58
+
59
+ // Built-in: an explicit `runtype:` prefix wrapping a TypeID.
60
+ if (prefix === "runtype") {
61
+ const detected = detectRuntypeTypeId(rest);
62
+ if (detected) return detected;
63
+ throw new Error(
64
+ `[Persona] target "runtype:${rest}" is not a valid Runtype agent_/flow_ id.`,
65
+ );
66
+ }
67
+
68
+ const resolver = targetProviders?.[prefix];
69
+ if (!resolver) {
70
+ throw new Error(
71
+ `[Persona] No target provider registered for "${prefix}". ` +
72
+ `Add a \`targetProviders.${prefix}\` resolver, or use a Runtype agent_/flow_ id.`,
73
+ );
74
+ }
75
+ return { kind: "payload", payload: resolver(rest).payload };
76
+ }
77
+
78
+ // No prefix: a bare Runtype TypeID is self-describing.
79
+ const detected = detectRuntypeTypeId(trimmed);
80
+ if (detected) return detected;
81
+
82
+ // Bare, non-TypeID name: only resolvable via an explicit default resolver.
83
+ const fallback = targetProviders?.default;
84
+ if (fallback) return { kind: "payload", payload: fallback(trimmed).payload };
85
+
86
+ throw new Error(
87
+ `[Persona] target "${trimmed}" has no provider prefix and is not a Runtype agent_/flow_ id. ` +
88
+ `Use "<provider>:${trimmed}", a Runtype TypeID, or register a \`targetProviders.default\` resolver.`,
89
+ );
90
+ }