@cef-ai/widget-runtime 0.2.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 (56) hide show
  1. package/README.md +34 -0
  2. package/dist/audio/capture-upload.d.ts +116 -0
  3. package/dist/audio/capture-upload.js +347 -0
  4. package/dist/audio/clip-registry.d.ts +20 -0
  5. package/dist/audio/clip-registry.js +47 -0
  6. package/dist/audio/ddc-delegation.d.ts +18 -0
  7. package/dist/audio/ddc-delegation.js +64 -0
  8. package/dist/audio/mic.d.ts +1 -0
  9. package/dist/audio/mic.js +23 -0
  10. package/dist/audio/segment.d.ts +3 -0
  11. package/dist/audio/segment.js +24 -0
  12. package/dist/audio/wav.d.ts +2 -0
  13. package/dist/audio/wav.js +27 -0
  14. package/dist/bridge-wallet.d.ts +46 -0
  15. package/dist/bridge-wallet.js +80 -0
  16. package/dist/bridge.d.ts +6 -0
  17. package/dist/bridge.js +15 -0
  18. package/dist/build-tools.d.ts +4 -0
  19. package/dist/build-tools.js +12 -0
  20. package/dist/entry.d.ts +11 -0
  21. package/dist/entry.js +159 -0
  22. package/dist/host-bridge.d.ts +19 -0
  23. package/dist/host-bridge.js +214 -0
  24. package/dist/index.d.ts +25 -0
  25. package/dist/index.js +21 -0
  26. package/dist/kinds/composite.d.ts +7 -0
  27. package/dist/kinds/composite.js +206 -0
  28. package/dist/kinds/conversation.d.ts +7 -0
  29. package/dist/kinds/conversation.js +667 -0
  30. package/dist/kinds/dashboard.d.ts +2 -0
  31. package/dist/kinds/dashboard.js +53 -0
  32. package/dist/kinds/list.d.ts +2 -0
  33. package/dist/kinds/list.js +22 -0
  34. package/dist/kinds/record.d.ts +2 -0
  35. package/dist/kinds/record.js +14 -0
  36. package/dist/kinds/submit.d.ts +6 -0
  37. package/dist/kinds/submit.js +481 -0
  38. package/dist/manifest.d.ts +32 -0
  39. package/dist/manifest.js +47 -0
  40. package/dist/mount.d.ts +19 -0
  41. package/dist/mount.js +48 -0
  42. package/dist/render-helpers.d.ts +9 -0
  43. package/dist/render-helpers.js +42 -0
  44. package/dist/runtime.d.ts +12 -0
  45. package/dist/runtime.js +36 -0
  46. package/dist/standalone-wallet.d.ts +21 -0
  47. package/dist/standalone-wallet.js +102 -0
  48. package/dist/types.d.ts +245 -0
  49. package/dist/types.js +7 -0
  50. package/dist/validate-config.d.ts +2 -0
  51. package/dist/validate-config.js +74 -0
  52. package/dist/vault-client.d.ts +45 -0
  53. package/dist/vault-client.js +122 -0
  54. package/dist/widget-runtime.js +348 -0
  55. package/dist/widget.html +33 -0
  56. package/package.json +43 -0
@@ -0,0 +1,122 @@
1
+ // packages/widget-runtime/src/vault-client.ts
2
+ import { AgentNotConnectedError } from './types.js';
3
+ /**
4
+ * Normalise the raw result from cubbies.query into a uniform QueryResult.
5
+ * The platform returns `{columns: string[], rows: unknown[][]}` (positional).
6
+ * Tolerates an array-of-objects shape for forward compatibility.
7
+ */
8
+ function normaliseQueryResult(raw) {
9
+ if (raw && typeof raw === 'object' && 'columns' in raw && 'rows' in raw) {
10
+ const cols = raw.columns;
11
+ const rows = raw.rows;
12
+ if (Array.isArray(cols) && Array.isArray(rows)) {
13
+ return { columns: cols.map(String), rows: rows, meta: { rowCount: rows.length } };
14
+ }
15
+ }
16
+ if (Array.isArray(raw)) {
17
+ // Array of row objects — derive columns from first row keys.
18
+ const keys = raw.length > 0 ? Object.keys(raw[0]) : [];
19
+ const rows = raw.map((r) => keys.map((k) => r[k]));
20
+ return { columns: keys, rows, meta: { rowCount: raw.length } };
21
+ }
22
+ // Fallback: empty result.
23
+ console.warn('[widget-runtime] unrecognized cubby query result shape; treating as empty', raw);
24
+ return { columns: [], rows: [], meta: { rowCount: 0 } };
25
+ }
26
+ /**
27
+ * Map a cubbies.query rejection to AgentNotConnectedError when the error
28
+ * indicates a 404 / agent-not-connected condition.
29
+ */
30
+ function mapQueryError(err, agentId) {
31
+ if (err && typeof err === 'object') {
32
+ const status = err.status;
33
+ const message = err.message;
34
+ if (status === 404 ||
35
+ (typeof message === 'string' && /not.?connected|404/i.test(message))) {
36
+ throw new AgentNotConnectedError(agentId);
37
+ }
38
+ }
39
+ throw err;
40
+ }
41
+ export function createVaultClient(manifest, makeSdk) {
42
+ let sdk;
43
+ let vaultP;
44
+ let agentHandleP;
45
+ const getSdk = () => (sdk ?? (sdk = makeSdk()));
46
+ const getVault = () => {
47
+ // `onboard: false` preserves the old lightweight ensure(): claim the vault
48
+ // with a wallet-built delegation token, skipping the gateway onboarding
49
+ // status/bootstrap round-trip (widgets assume a provisioned wallet).
50
+ vaultP ?? (vaultP = getSdk().vault.ensure({ onboard: false }).catch((err) => {
51
+ vaultP = undefined;
52
+ throw err;
53
+ }));
54
+ return vaultP;
55
+ };
56
+ // Memoised agent-connection handle for cubby reads. Cached so polling
57
+ // queries don't re-fetch the connection record on every call (matching the
58
+ // single-request-per-query cost of the prior path). Reset on (re)connect
59
+ // and on read failure so a stale handle can't pin a disconnected state.
60
+ const getAgentHandle = () => {
61
+ agentHandleP ?? (agentHandleP = getVault()
62
+ .then((vault) => vault.agents.get(manifest.agentId))
63
+ .catch((err) => {
64
+ agentHandleP = undefined;
65
+ throw err;
66
+ }));
67
+ return agentHandleP;
68
+ };
69
+ const resolveSql = (ref) => manifest.queries.find((q) => q.id === ref)?.sql ?? ref;
70
+ return {
71
+ async query(ref, params) {
72
+ // Cubby reads go through vault-sdk's agentId-keyed endpoint. This was
73
+ // briefly routed via @cef-ai/client-sdk because vault-sdk@0.5.0
74
+ // percent-encoded the agentId ':' → '%3A' → 404; fixed in 0.5.1.
75
+ let raw;
76
+ try {
77
+ const handle = await getAgentHandle();
78
+ // vault-sdk 2.x throws synchronously from `.cubby(alias)` when the
79
+ // alias isn't in the connection's declared aliases (e.g. the agent
80
+ // isn't connected yet, so the handle carries no aliases). Map that to
81
+ // AgentNotConnectedError instead of letting an opaque throw escape.
82
+ let cubbyHandle;
83
+ try {
84
+ cubbyHandle = handle.cubby(manifest.cubbyAlias);
85
+ }
86
+ catch {
87
+ throw new AgentNotConnectedError(manifest.agentId);
88
+ }
89
+ raw = await cubbyHandle.query(resolveSql(ref), params ?? []);
90
+ }
91
+ catch (err) {
92
+ agentHandleP = undefined; // force a fresh handle on the next read
93
+ if (err instanceof AgentNotConnectedError)
94
+ throw err;
95
+ mapQueryError(err, manifest.agentId);
96
+ }
97
+ return normaliseQueryResult(raw);
98
+ },
99
+ async publish(type, payload, context) {
100
+ const vault = await getVault();
101
+ return vault.scope(manifest.scope).publish({ type, context: context ?? '', payload });
102
+ },
103
+ async connectAgent() {
104
+ const vault = await getVault();
105
+ // 2.x connect is card-only: pass the agentId + scope straight through.
106
+ // The vault-api resolves the manifest from the DDC registry and the SDK
107
+ // signs the GAR agreement internally — no marketplace.getAgent() fetch.
108
+ await vault.agents.connect({ agentId: manifest.agentId, scope: manifest.scope });
109
+ agentHandleP = undefined; // drop any cached pre-connect handle
110
+ },
111
+ async agentStatus() {
112
+ try {
113
+ const vault = await getVault();
114
+ const handle = await vault.agents.get(manifest.agentId);
115
+ return handle.status === 'active' ? 'connected' : 'disconnected';
116
+ }
117
+ catch {
118
+ return 'disconnected';
119
+ }
120
+ },
121
+ };
122
+ }