@celljs/ta-harness 4.5.0 → 4.6.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 (70) hide show
  1. package/README.md +6 -5
  2. package/esm/node/brand-extension.js +2 -2
  3. package/esm/node/bug-report.js +2 -2
  4. package/esm/node/extensions.js +1 -1
  5. package/esm/node/index.d.ts +2 -0
  6. package/esm/node/index.js +2 -0
  7. package/esm/node/index.js.map +1 -1
  8. package/esm/node/pi-types.d.ts +1 -0
  9. package/esm/node/serve-state.d.ts +18 -0
  10. package/esm/node/serve-state.js +44 -0
  11. package/esm/node/serve-state.js.map +1 -0
  12. package/esm/node/ta-brand.d.ts +2 -0
  13. package/esm/node/ta-brand.js +12 -0
  14. package/esm/node/ta-brand.js.map +1 -1
  15. package/esm/node/ta-cli.js +152 -6
  16. package/esm/node/ta-cli.js.map +1 -1
  17. package/esm/node/ta-header.js +3 -3
  18. package/esm/node/ta-packages.d.ts +67 -0
  19. package/esm/node/ta-packages.js +173 -0
  20. package/esm/node/ta-packages.js.map +1 -0
  21. package/lib/node/brand-extension.js +2 -2
  22. package/lib/node/brand-extension.spec.js +3 -3
  23. package/lib/node/bug-report.js +2 -2
  24. package/lib/node/extensions.js +1 -1
  25. package/lib/node/index.d.ts +2 -0
  26. package/lib/node/index.d.ts.map +1 -1
  27. package/lib/node/index.js +2 -0
  28. package/lib/node/index.js.map +1 -1
  29. package/lib/node/pi-types.d.ts +1 -0
  30. package/lib/node/pi-types.d.ts.map +1 -1
  31. package/lib/node/serve-state.d.ts +19 -0
  32. package/lib/node/serve-state.d.ts.map +1 -0
  33. package/lib/node/serve-state.js +47 -0
  34. package/lib/node/serve-state.js.map +1 -0
  35. package/lib/node/serve-state.spec.d.ts +2 -0
  36. package/lib/node/serve-state.spec.d.ts.map +1 -0
  37. package/lib/node/serve-state.spec.js +35 -0
  38. package/lib/node/serve-state.spec.js.map +1 -0
  39. package/lib/node/ta-brand.d.ts +2 -0
  40. package/lib/node/ta-brand.d.ts.map +1 -1
  41. package/lib/node/ta-brand.js +12 -0
  42. package/lib/node/ta-brand.js.map +1 -1
  43. package/lib/node/ta-cli.d.ts.map +1 -1
  44. package/lib/node/ta-cli.js +152 -6
  45. package/lib/node/ta-cli.js.map +1 -1
  46. package/lib/node/ta-header.js +3 -3
  47. package/lib/node/ta-header.spec.js +2 -2
  48. package/lib/node/ta-packages.d.ts +68 -0
  49. package/lib/node/ta-packages.d.ts.map +1 -0
  50. package/lib/node/ta-packages.js +172 -0
  51. package/lib/node/ta-packages.js.map +1 -0
  52. package/lib/node/ta-packages.spec.d.ts +2 -0
  53. package/lib/node/ta-packages.spec.d.ts.map +1 -0
  54. package/lib/node/ta-packages.spec.js +89 -0
  55. package/lib/node/ta-packages.spec.js.map +1 -0
  56. package/package.json +5 -4
  57. package/src/node/brand-extension.spec.ts +3 -3
  58. package/src/node/brand-extension.ts +2 -2
  59. package/src/node/bug-report.ts +2 -2
  60. package/src/node/extensions.ts +1 -1
  61. package/src/node/index.ts +2 -0
  62. package/src/node/pi-types.ts +1 -0
  63. package/src/node/serve-state.spec.ts +36 -0
  64. package/src/node/serve-state.ts +51 -0
  65. package/src/node/ta-brand.ts +10 -0
  66. package/src/node/ta-cli.ts +155 -6
  67. package/src/node/ta-header.spec.ts +2 -2
  68. package/src/node/ta-header.ts +3 -3
  69. package/src/node/ta-packages.spec.ts +95 -0
  70. package/src/node/ta-packages.ts +200 -0
@@ -0,0 +1,200 @@
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname, join, sep } from 'node:path';
4
+ import type { LoadExtensionsResult } from './pi-types';
5
+
6
+ /** The options of pi's resource loader that load the built-in packages. */
7
+ export interface TaPackageLoaderOptions {
8
+ additionalExtensionPaths: string[];
9
+ extensionsOverride(result: LoadExtensionsResult): LoadExtensionsResult;
10
+ skillsOverride<T extends { filePath: string }, D extends { path?: string }>(result: { skills: T[]; diagnostics: D[] }): { skills: T[]; diagnostics: D[] };
11
+ promptsOverride<T extends { filePath: string }, D extends { path?: string }>(result: {
12
+ prompts: T[];
13
+ diagnostics: D[];
14
+ }): { prompts: T[]; diagnostics: D[] };
15
+ }
16
+
17
+ /** A pi package that comes with ta. */
18
+ export interface TaBuiltinPackage {
19
+ /** The `name` of its package.json. */
20
+ name: string;
21
+ root: string;
22
+ /** It has what pi loads: `pi.extensions`, `pi.skills` or `pi.prompts` of its package.json. */
23
+ resources: boolean;
24
+ }
25
+
26
+ /** ta's own settings, next to pi's in the agent directory. */
27
+ interface TaSettings {
28
+ /** Built-in packages the user turned off. */
29
+ disabledPackages?: string[];
30
+ /**
31
+ * The directories of other packages turned off that pi loads nothing of, only their web extension:
32
+ * pi's own settings keep what it loads (see WebExtensionCatalog), not a web extension.
33
+ */
34
+ disabledWeb?: string[];
35
+ }
36
+
37
+ /** Commands of pi that manage packages and take no `--extension`. */
38
+ const PACKAGE_COMMANDS = new Set(['install', 'remove', 'uninstall', 'update', 'list', 'config']);
39
+
40
+ /**
41
+ * The pi packages that come with ta. They are on unless the user turned them off; they cannot be
42
+ * removed. The build of apps/ta copies each to `packages/<name without scope>` of pi's package
43
+ * directory (PI_PACKAGE_DIR); in the repository they are the workspace packages of {@link BUILTIN}.
44
+ */
45
+ export namespace TaPackages {
46
+ export const BUILTIN: readonly string[] = ['@celljs/ta-workspace', '@celljs/ta-channels', '@celljs/ta-extensions'];
47
+
48
+ /** In the agent directory. */
49
+ export const SETTINGS_FILE = 'ta.json';
50
+
51
+ export function builtins(packageDir = process.env.PI_PACKAGE_DIR): TaBuiltinPackage[] {
52
+ const dir = packageDir ? join(packageDir, 'packages') : undefined;
53
+ return dir && existsSync(dir) ? described(subdirectories(dir)) : workspace();
54
+ }
55
+
56
+ /** Not built: those of {@link BUILTIN} that are installed next to this file, in the repository. */
57
+ export function workspace(): TaBuiltinPackage[] {
58
+ const resolve = createRequire(__filename).resolve;
59
+ return described(
60
+ BUILTIN.flatMap(name => {
61
+ try {
62
+ return [dirname(resolve(`${name}/package.json`))];
63
+ } catch {
64
+ return [];
65
+ }
66
+ }),
67
+ );
68
+ }
69
+
70
+ /** The directory of a package in pi's package directory. */
71
+ export function directoryName(name: string): string {
72
+ return name.slice(name.lastIndexOf('/') + 1);
73
+ }
74
+
75
+ export function disabled(agentDir: string): Set<string> {
76
+ return new Set(readSettings(agentDir).disabledPackages ?? []);
77
+ }
78
+
79
+ export function setEnabled(agentDir: string, name: string, enabled: boolean): void {
80
+ toggle(agentDir, 'disabledPackages', name, enabled);
81
+ }
82
+
83
+ /** The directories of the packages with only a web extension that the user turned off. */
84
+ export function disabledWeb(agentDir: string): Set<string> {
85
+ return new Set(readSettings(agentDir).disabledWeb ?? []);
86
+ }
87
+
88
+ export function setWebEnabled(agentDir: string, root: string, enabled: boolean): void {
89
+ toggle(agentDir, 'disabledWeb', root, enabled);
90
+ }
91
+
92
+ export function enabled(agentDir: string, packageDir?: string): TaBuiltinPackage[] {
93
+ const off = disabled(agentDir);
94
+ return builtins(packageDir).filter(pkg => !off.has(pkg.name));
95
+ }
96
+
97
+ /** The directories to load pi extensions, skills and prompts from, as `--extension` and pi's `additionalExtensionPaths` take them. */
98
+ export function extensionPaths(agentDir: string, packageDir?: string): string[] {
99
+ return enabled(agentDir, packageDir)
100
+ .filter(pkg => pkg.resources)
101
+ .map(pkg => pkg.root);
102
+ }
103
+
104
+ /**
105
+ * For pi's resource loader of a session: it loads every built-in package, its pi extensions, skills
106
+ * and prompt templates, and keeps those of the packages that are on, read again on each reload, so
107
+ * that a reload follows the setting.
108
+ */
109
+ export function loaderOptions(agentDir: string, packageDir?: string): TaPackageLoaderOptions {
110
+ const roots = builtins(packageDir)
111
+ .filter(pkg => pkg.resources)
112
+ .map(pkg => pkg.root);
113
+ /** Whether a file is not of a built-in package that is off. */
114
+ const keeper = () => {
115
+ const on = new Set(extensionPaths(agentDir, packageDir));
116
+ const off = roots.filter(root => !on.has(root));
117
+ return (path: string | undefined) => !path || !off.some(root => path === root || path.startsWith(`${root}${sep}`));
118
+ };
119
+ return {
120
+ additionalExtensionPaths: roots,
121
+ extensionsOverride: result => {
122
+ const keep = keeper();
123
+ return {
124
+ ...result,
125
+ extensions: result.extensions.filter(extension => keep(extension.resolvedPath) && keep(extension.path)),
126
+ errors: result.errors.filter(error => keep(error.path)),
127
+ };
128
+ },
129
+ skillsOverride: result => {
130
+ const keep = keeper();
131
+ return { ...result, skills: result.skills.filter(skill => keep(skill.filePath)), diagnostics: result.diagnostics.filter(d => keep(d.path)) };
132
+ },
133
+ promptsOverride: result => {
134
+ const keep = keeper();
135
+ return {
136
+ ...result,
137
+ prompts: result.prompts.filter(prompt => keep(prompt.filePath)),
138
+ diagnostics: result.diagnostics.filter(d => keep(d.path)),
139
+ };
140
+ },
141
+ };
142
+ }
143
+
144
+ /** pi's command line with the built-in packages that are on, unless it manages packages or loads no extensions. */
145
+ export function withExtensions(argv: string[], agentDir: string, packageDir?: string): string[] {
146
+ if (PACKAGE_COMMANDS.has(argv[0]) || argv.includes('--no-extensions') || argv.includes('-ne')) {
147
+ return argv;
148
+ }
149
+ return [...argv, ...extensionPaths(agentDir, packageDir).flatMap(root => ['--extension', root])];
150
+ }
151
+
152
+ function describe(root: string): TaBuiltinPackage | undefined {
153
+ try {
154
+ const manifest = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')) as { name?: string; pi?: Record<string, unknown> };
155
+ const declared = (key: string) => {
156
+ const paths = manifest.pi?.[key];
157
+ return Array.isArray(paths) && paths.length > 0;
158
+ };
159
+ return { name: manifest.name ?? directoryName(root), root, resources: declared('extensions') || declared('skills') || declared('prompts') };
160
+ } catch {
161
+ return undefined;
162
+ }
163
+ }
164
+
165
+ function subdirectories(dir: string): string[] {
166
+ return readdirSync(dir, { withFileTypes: true })
167
+ .filter(entry => entry.isDirectory() && !entry.name.startsWith('.'))
168
+ .map(entry => join(dir, entry.name))
169
+ .sort();
170
+ }
171
+
172
+ function described(roots: string[]): TaBuiltinPackage[] {
173
+ return roots.flatMap(root => {
174
+ const pkg = describe(root);
175
+ return pkg ? [pkg] : [];
176
+ });
177
+ }
178
+
179
+ function toggle(agentDir: string, key: 'disabledPackages' | 'disabledWeb', value: string, enabled: boolean): void {
180
+ const settings = readSettings(agentDir);
181
+ const off = new Set(settings[key] ?? []);
182
+ if (enabled) {
183
+ off.delete(value);
184
+ } else {
185
+ off.add(value);
186
+ }
187
+ const file = join(agentDir, SETTINGS_FILE);
188
+ mkdirSync(agentDir, { recursive: true });
189
+ writeFileSync(`${file}.tmp`, `${JSON.stringify({ ...settings, [key]: off.size > 0 || settings[key] ? [...off].sort() : undefined }, null, 2)}\n`);
190
+ renameSync(`${file}.tmp`, file);
191
+ }
192
+
193
+ function readSettings(agentDir: string): TaSettings {
194
+ try {
195
+ return JSON.parse(readFileSync(join(agentDir, SETTINGS_FILE), 'utf8')) as TaSettings;
196
+ } catch {
197
+ return {};
198
+ }
199
+ }
200
+ }