coaiajs 0.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.
- package/CLAUDE.md +56 -0
- package/KINSHIP.md +63 -0
- package/README.md +148 -0
- package/articles/academic/creative-orientation-vs-problem-solving.md +177 -0
- package/articles/academic/jsonl-knowledge-graphs-agent-memory.md +142 -0
- package/articles/academic/langfuse-observability-llm-pipelines.md +144 -0
- package/articles/academic/medicine-wheel-software-architecture.md +163 -0
- package/articles/academic/mmot-autonomous-agents.md +156 -0
- package/articles/academic/model-context-protocol-interagent.md +161 -0
- package/articles/academic/pde-prompt-decomposition.md +186 -0
- package/articles/academic/structural-tension-in-ai-agents.md +134 -0
- package/articles/reviews/mcp-protocol-design-review.md +170 -0
- package/articles/reviews/observability-ai-systems-review.md +176 -0
- package/articles/reviews/prompt-engineering-decomposition-review.md +184 -0
- package/articles/surveys/agent-orchestration-survey.md +186 -0
- package/articles/surveys/knowledge-graph-storage-survey.md +204 -0
- package/articles/surveys/structural-tension-methodology-survey.md +154 -0
- package/articles/technical/aws-sdk-v3-polly.md +270 -0
- package/articles/technical/commander-cli-framework.md +262 -0
- package/articles/technical/dotenv-config-patterns.md +360 -0
- package/articles/technical/ioredis-vs-redis.md +142 -0
- package/articles/technical/langfuse-js-sdk-vs-rest.md +191 -0
- package/articles/technical/mcp-sdk-typescript.md +291 -0
- package/articles/technical/octokit-github-api.md +293 -0
- package/articles/technical/openai-sdk-modern.md +231 -0
- package/articles/technical/yaml-parsing-node.md +266 -0
- package/articles/technical/zod-runtime-validation.md +212 -0
- package/dist/mcp/config.d.ts +14 -0
- package/dist/mcp/config.js +185 -0
- package/dist/mcp/server.d.ts +3 -0
- package/dist/mcp/server.js +324 -0
- package/dist/mcp/tools/coaiapy-tools.d.ts +8 -0
- package/dist/mcp/tools/coaiapy-tools.js +326 -0
- package/dist/mcp/tools/index.d.ts +3 -0
- package/dist/mcp/tools/index.js +3 -0
- package/dist/src/audio.d.ts +12 -0
- package/dist/src/audio.js +57 -0
- package/dist/src/cli-helpers.d.ts +9 -0
- package/dist/src/cli-helpers.js +78 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.js +1086 -0
- package/dist/src/config.d.ts +15 -0
- package/dist/src/config.js +145 -0
- package/dist/src/environment.d.ts +44 -0
- package/dist/src/environment.js +146 -0
- package/dist/src/github.d.ts +47 -0
- package/dist/src/github.js +79 -0
- package/dist/src/langfuse/client.d.ts +30 -0
- package/dist/src/langfuse/client.js +75 -0
- package/dist/src/langfuse/comments.d.ts +16 -0
- package/dist/src/langfuse/comments.js +36 -0
- package/dist/src/langfuse/datasets.d.ts +19 -0
- package/dist/src/langfuse/datasets.js +140 -0
- package/dist/src/langfuse/index.d.ts +13 -0
- package/dist/src/langfuse/index.js +10 -0
- package/dist/src/langfuse/media.d.ts +11 -0
- package/dist/src/langfuse/media.js +167 -0
- package/dist/src/langfuse/observations.d.ts +18 -0
- package/dist/src/langfuse/observations.js +113 -0
- package/dist/src/langfuse/prompts.d.ts +14 -0
- package/dist/src/langfuse/prompts.js +127 -0
- package/dist/src/langfuse/scores.d.ts +54 -0
- package/dist/src/langfuse/scores.js +366 -0
- package/dist/src/langfuse/traces.d.ts +29 -0
- package/dist/src/langfuse/traces.js +225 -0
- package/dist/src/llm.d.ts +23 -0
- package/dist/src/llm.js +76 -0
- package/dist/src/narrative/graph-manager.d.ts +106 -0
- package/dist/src/narrative/graph-manager.js +915 -0
- package/dist/src/narrative/index.d.ts +9 -0
- package/dist/src/narrative/index.js +7 -0
- package/dist/src/narrative/markdown-export.d.ts +21 -0
- package/dist/src/narrative/markdown-export.js +383 -0
- package/dist/src/narrative/tool-definitions.d.ts +21 -0
- package/dist/src/narrative/tool-definitions.js +588 -0
- package/dist/src/narrative/tool-handlers.d.ts +11 -0
- package/dist/src/narrative/tool-handlers.js +462 -0
- package/dist/src/narrative/types.d.ts +2 -0
- package/dist/src/narrative/types.js +2 -0
- package/dist/src/narrative/validation.d.ts +29 -0
- package/dist/src/narrative/validation.js +144 -0
- package/dist/src/pde/index.d.ts +8 -0
- package/dist/src/pde/index.js +8 -0
- package/dist/src/pde/mcp-handlers.d.ts +7 -0
- package/dist/src/pde/mcp-handlers.js +300 -0
- package/dist/src/pde/mcp-tools.d.ts +15 -0
- package/dist/src/pde/mcp-tools.js +191 -0
- package/dist/src/pde/session-manager.d.ts +32 -0
- package/dist/src/pde/session-manager.js +205 -0
- package/dist/src/pde/stc-mapper.d.ts +26 -0
- package/dist/src/pde/stc-mapper.js +234 -0
- package/dist/src/pipeline/index.d.ts +2 -0
- package/dist/src/pipeline/index.js +3 -0
- package/dist/src/pipeline/template-engine.d.ts +20 -0
- package/dist/src/pipeline/template-engine.js +361 -0
- package/dist/src/planning/index.d.ts +7 -0
- package/dist/src/planning/index.js +7 -0
- package/dist/src/planning/mcp-handlers.d.ts +7 -0
- package/dist/src/planning/mcp-handlers.js +310 -0
- package/dist/src/planning/mcp-tools.d.ts +15 -0
- package/dist/src/planning/mcp-tools.js +145 -0
- package/dist/src/planning/plan-parser.d.ts +35 -0
- package/dist/src/planning/plan-parser.js +506 -0
- package/dist/src/redis.d.ts +21 -0
- package/dist/src/redis.js +78 -0
- package/dist/src/types.d.ts +244 -0
- package/dist/src/types.js +8 -0
- package/mcp/config.ts +196 -0
- package/mcp/server.ts +402 -0
- package/mcp/tools/coaiapy-tools.ts +364 -0
- package/mcp/tools/index.ts +4 -0
- package/package.json +52 -0
- package/rispecs/00-coaiajs-platform.spec.md +102 -0
- package/rispecs/01-core-config.spec.md +89 -0
- package/rispecs/02-redis-module.spec.md +62 -0
- package/rispecs/03-langfuse-module.spec.md +101 -0
- package/rispecs/04-narrative-engine.spec.md +185 -0
- package/rispecs/05-pde-engine.spec.md +112 -0
- package/rispecs/06-planning-engine.spec.md +128 -0
- package/rispecs/07-pipeline-templates.spec.md +97 -0
- package/rispecs/08-cli-interface.spec.md +134 -0
- package/rispecs/09-mcp-server.spec.md +140 -0
- package/rispecs/10-audio-module.spec.md +63 -0
- package/rispecs/KINSHIP.md +56 -0
- package/rispecs/README.md +100 -0
- package/src/audio.ts +76 -0
- package/src/cli-helpers.ts +86 -0
- package/src/cli.ts +1223 -0
- package/src/config.ts +172 -0
- package/src/environment.ts +171 -0
- package/src/github.ts +143 -0
- package/src/langfuse/client.ts +105 -0
- package/src/langfuse/comments.ts +52 -0
- package/src/langfuse/datasets.ts +178 -0
- package/src/langfuse/index.ts +33 -0
- package/src/langfuse/media.ts +193 -0
- package/src/langfuse/observations.ts +131 -0
- package/src/langfuse/prompts.ts +157 -0
- package/src/langfuse/scores.ts +456 -0
- package/src/langfuse/traces.ts +276 -0
- package/src/llm.ts +106 -0
- package/src/narrative/graph-manager.ts +1358 -0
- package/src/narrative/index.ts +32 -0
- package/src/narrative/markdown-export.ts +535 -0
- package/src/narrative/tool-definitions.ts +635 -0
- package/src/narrative/tool-handlers.ts +528 -0
- package/src/narrative/types.ts +9 -0
- package/src/narrative/validation.ts +179 -0
- package/src/pde/index.ts +8 -0
- package/src/pde/mcp-handlers.ts +359 -0
- package/src/pde/mcp-tools.ts +201 -0
- package/src/pde/session-manager.ts +248 -0
- package/src/pde/stc-mapper.ts +298 -0
- package/src/pipeline/index.ts +7 -0
- package/src/pipeline/template-engine.ts +398 -0
- package/src/planning/index.ts +13 -0
- package/src/planning/mcp-handlers.ts +369 -0
- package/src/planning/mcp-tools.ts +155 -0
- package/src/planning/plan-parser.ts +587 -0
- package/src/redis.ts +97 -0
- package/src/types.ts +280 -0
- package/tsconfig.json +26 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// coaiajs/src/config.ts — Config management
|
|
2
|
+
// Priority: env vars > .env > coaia.json > defaults
|
|
3
|
+
|
|
4
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
5
|
+
import { resolve, join } from 'node:path';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import dotenv from 'dotenv';
|
|
8
|
+
import type { CoaiaConfig } from './types.js';
|
|
9
|
+
|
|
10
|
+
const CONFIG_FILENAMES = ['coaia.json', '.coaia/config.json'];
|
|
11
|
+
|
|
12
|
+
/** Search for an existing coaia.json config file. */
|
|
13
|
+
export function findExistingConfig(): string | null {
|
|
14
|
+
const searchDirs = [process.cwd(), homedir()];
|
|
15
|
+
|
|
16
|
+
for (const dir of searchDirs) {
|
|
17
|
+
for (const filename of CONFIG_FILENAMES) {
|
|
18
|
+
const candidate = join(dir, filename);
|
|
19
|
+
if (existsSync(candidate)) {
|
|
20
|
+
return candidate;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Deep-merge two config objects; override wins on conflicts. */
|
|
28
|
+
export function mergeConfigs(
|
|
29
|
+
base: Partial<CoaiaConfig>,
|
|
30
|
+
override: Partial<CoaiaConfig>,
|
|
31
|
+
): CoaiaConfig {
|
|
32
|
+
const result: Record<string, unknown> = { ...base };
|
|
33
|
+
|
|
34
|
+
for (const [key, val] of Object.entries(override)) {
|
|
35
|
+
if (
|
|
36
|
+
val !== null &&
|
|
37
|
+
val !== undefined &&
|
|
38
|
+
typeof val === 'object' &&
|
|
39
|
+
!Array.isArray(val) &&
|
|
40
|
+
typeof result[key] === 'object' &&
|
|
41
|
+
result[key] !== null &&
|
|
42
|
+
!Array.isArray(result[key])
|
|
43
|
+
) {
|
|
44
|
+
result[key] = mergeConfigs(
|
|
45
|
+
result[key] as Partial<CoaiaConfig>,
|
|
46
|
+
val as Partial<CoaiaConfig>,
|
|
47
|
+
);
|
|
48
|
+
} else if (val !== undefined) {
|
|
49
|
+
result[key] = val;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return result as CoaiaConfig;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function loadJsonConfig(filePath: string): Partial<CoaiaConfig> {
|
|
56
|
+
try {
|
|
57
|
+
const raw = readFileSync(filePath, 'utf-8');
|
|
58
|
+
return JSON.parse(raw) as Partial<CoaiaConfig>;
|
|
59
|
+
} catch {
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function envOverrides(): Partial<CoaiaConfig> {
|
|
65
|
+
const cfg: Partial<CoaiaConfig> = {};
|
|
66
|
+
const env = process.env;
|
|
67
|
+
|
|
68
|
+
// Redis
|
|
69
|
+
if (env['REDIS_URL'] || env['REDIS_HOST'] || env['UPSTASH_REDIS_REST_URL']) {
|
|
70
|
+
cfg.redis = {
|
|
71
|
+
url: env['REDIS_URL'],
|
|
72
|
+
host: env['REDIS_HOST'],
|
|
73
|
+
port: env['REDIS_PORT'] ? parseInt(env['REDIS_PORT'], 10) : undefined,
|
|
74
|
+
password: env['REDIS_PASSWORD'],
|
|
75
|
+
upstashUrl: env['UPSTASH_REDIS_REST_URL'],
|
|
76
|
+
upstashToken: env['UPSTASH_REDIS_REST_TOKEN'],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Langfuse
|
|
81
|
+
if (env['LANGFUSE_PUBLIC_KEY'] || env['LANGFUSE_SECRET_KEY']) {
|
|
82
|
+
cfg.langfuse = {
|
|
83
|
+
publicKey: env['LANGFUSE_PUBLIC_KEY'],
|
|
84
|
+
secretKey: env['LANGFUSE_SECRET_KEY'],
|
|
85
|
+
baseUrl: env['LANGFUSE_BASE_URL'] ?? env['LANGFUSE_HOST'],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// OpenAI
|
|
90
|
+
if (env['OPENAI_API_KEY']) {
|
|
91
|
+
cfg.openai = {
|
|
92
|
+
apiKey: env['OPENAI_API_KEY'],
|
|
93
|
+
model: env['OPENAI_MODEL'] ?? env['LLM_MODEL'],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// AWS
|
|
98
|
+
if (env['AWS_ACCESS_KEY_ID']) {
|
|
99
|
+
cfg.aws = {
|
|
100
|
+
accessKeyId: env['AWS_ACCESS_KEY_ID'],
|
|
101
|
+
secretAccessKey: env['AWS_SECRET_ACCESS_KEY'],
|
|
102
|
+
region: env['AWS_REGION'] ?? env['AWS_DEFAULT_REGION'],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// GitHub
|
|
107
|
+
if (env['GITHUB_TOKEN'] || env['GH_TOKEN']) {
|
|
108
|
+
cfg.github = {
|
|
109
|
+
token: env['GITHUB_TOKEN'] ?? env['GH_TOKEN'],
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return cfg;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Read and assemble the full CoaiaConfig.
|
|
118
|
+
* Priority: env vars > .env file > coaia.json > defaults.
|
|
119
|
+
*/
|
|
120
|
+
export function readConfig(envPath?: string): CoaiaConfig {
|
|
121
|
+
// 1. Load .env (lowest priority file source)
|
|
122
|
+
const dotenvPath = envPath ?? resolve(process.cwd(), '.env');
|
|
123
|
+
if (existsSync(dotenvPath)) {
|
|
124
|
+
dotenv.config({ path: dotenvPath });
|
|
125
|
+
} else {
|
|
126
|
+
dotenv.config(); // default search
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 2. Load coaia.json
|
|
130
|
+
const configPath = findExistingConfig();
|
|
131
|
+
const jsonConfig = configPath ? loadJsonConfig(configPath) : {};
|
|
132
|
+
|
|
133
|
+
// 3. Env var overrides (highest priority)
|
|
134
|
+
const envCfg = envOverrides();
|
|
135
|
+
|
|
136
|
+
// 4. Merge: json < env
|
|
137
|
+
return mergeConfigs(jsonConfig, envCfg);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Singleton config instance, lazily loaded on first access. */
|
|
141
|
+
let _config: CoaiaConfig | null = null;
|
|
142
|
+
|
|
143
|
+
export function getConfig(): CoaiaConfig {
|
|
144
|
+
if (!_config) {
|
|
145
|
+
_config = readConfig();
|
|
146
|
+
}
|
|
147
|
+
return _config;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Reset singleton (useful in tests). */
|
|
151
|
+
export function resetConfig(): void {
|
|
152
|
+
_config = null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const config = new Proxy({} as CoaiaConfig, {
|
|
156
|
+
get(_target, prop: string) {
|
|
157
|
+
return getConfig()[prop];
|
|
158
|
+
},
|
|
159
|
+
ownKeys() {
|
|
160
|
+
return Object.keys(getConfig());
|
|
161
|
+
},
|
|
162
|
+
getOwnPropertyDescriptor(_target, prop: string) {
|
|
163
|
+
const cfg = getConfig();
|
|
164
|
+
if (prop in cfg) {
|
|
165
|
+
return { configurable: true, enumerable: true, value: cfg[prop] };
|
|
166
|
+
}
|
|
167
|
+
return undefined;
|
|
168
|
+
},
|
|
169
|
+
has(_target, prop: string) {
|
|
170
|
+
return prop in getConfig();
|
|
171
|
+
},
|
|
172
|
+
});
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// coaiajs/src/environment.ts — Environment management
|
|
2
|
+
// Parity with coaiapy's environment.py
|
|
3
|
+
|
|
4
|
+
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
|
|
5
|
+
import { resolve, join } from 'node:path';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
|
|
8
|
+
const ENV_FILENAME = '.coaia-env';
|
|
9
|
+
|
|
10
|
+
/** Well-known CoAIA environment variable names. */
|
|
11
|
+
const KNOWN_VARS = [
|
|
12
|
+
'COAIA_TRACE_ID',
|
|
13
|
+
'COAIA_SESSION_ID',
|
|
14
|
+
'COAIA_USER_ID',
|
|
15
|
+
'COAIA_PROJECT_ID',
|
|
16
|
+
'COAIA_ENV',
|
|
17
|
+
'COAIA_LOG_LEVEL',
|
|
18
|
+
'COAIA_CONFIG_PATH',
|
|
19
|
+
'COAIA_REDIS_URL',
|
|
20
|
+
'COAIA_PDE_DIR',
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
export type KnownVar = (typeof KNOWN_VARS)[number];
|
|
24
|
+
|
|
25
|
+
export interface EnvStore {
|
|
26
|
+
[key: string]: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class EnvironmentManager {
|
|
30
|
+
private store: EnvStore = {};
|
|
31
|
+
private filePath: string;
|
|
32
|
+
|
|
33
|
+
constructor(dir?: string) {
|
|
34
|
+
const baseDir = dir ?? process.cwd();
|
|
35
|
+
this.filePath = resolve(baseDir, ENV_FILENAME);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Initialize environment — load from file if it exists. */
|
|
39
|
+
init(): void {
|
|
40
|
+
if (existsSync(this.filePath)) {
|
|
41
|
+
this.load();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** List all environment variables in the store. */
|
|
46
|
+
list(): EnvStore {
|
|
47
|
+
return { ...this.store };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Get a single variable value. */
|
|
51
|
+
get(key: string): string | undefined {
|
|
52
|
+
// Check store first, fall back to process.env
|
|
53
|
+
return this.store[key] ?? process.env[key];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Set an environment variable (in-memory + process.env). */
|
|
57
|
+
set(key: string, value: string): void {
|
|
58
|
+
this.store[key] = value;
|
|
59
|
+
process.env[key] = value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Remove a variable from the store and process.env. */
|
|
63
|
+
unset(key: string): void {
|
|
64
|
+
delete this.store[key];
|
|
65
|
+
delete process.env[key];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Clear all stored variables. */
|
|
69
|
+
clear(): void {
|
|
70
|
+
for (const key of Object.keys(this.store)) {
|
|
71
|
+
delete process.env[key];
|
|
72
|
+
}
|
|
73
|
+
this.store = {};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Source variables into process.env (like `source .env`).
|
|
78
|
+
* Applies all stored vars to process.env.
|
|
79
|
+
*/
|
|
80
|
+
source(): void {
|
|
81
|
+
for (const [key, value] of Object.entries(this.store)) {
|
|
82
|
+
process.env[key] = value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Save the current store to .coaia-env (JSON format). */
|
|
87
|
+
save(): void {
|
|
88
|
+
writeFileSync(this.filePath, JSON.stringify(this.store, null, 2), 'utf-8');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Save as .env format. */
|
|
92
|
+
saveAsEnv(outPath?: string): void {
|
|
93
|
+
const target = outPath ?? this.filePath.replace(/\.coaia-env$/, '.coaia.env');
|
|
94
|
+
const lines = Object.entries(this.store)
|
|
95
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
96
|
+
.join('\n');
|
|
97
|
+
writeFileSync(target, lines + '\n', 'utf-8');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Load from .coaia-env file. Supports JSON and .env formats. */
|
|
101
|
+
private load(): void {
|
|
102
|
+
const raw = readFileSync(this.filePath, 'utf-8').trim();
|
|
103
|
+
|
|
104
|
+
// Try JSON first
|
|
105
|
+
if (raw.startsWith('{')) {
|
|
106
|
+
try {
|
|
107
|
+
this.store = JSON.parse(raw) as EnvStore;
|
|
108
|
+
return;
|
|
109
|
+
} catch {
|
|
110
|
+
// Fall through to .env parsing
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Parse .env format: KEY=VALUE lines
|
|
115
|
+
this.store = {};
|
|
116
|
+
for (const line of raw.split('\n')) {
|
|
117
|
+
const trimmed = line.trim();
|
|
118
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
119
|
+
const eqIdx = trimmed.indexOf('=');
|
|
120
|
+
if (eqIdx === -1) continue;
|
|
121
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
122
|
+
let value = trimmed.slice(eqIdx + 1).trim();
|
|
123
|
+
// Strip surrounding quotes
|
|
124
|
+
if (
|
|
125
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
126
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
127
|
+
) {
|
|
128
|
+
value = value.slice(1, -1);
|
|
129
|
+
}
|
|
130
|
+
this.store[key] = value;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Get the file path being used. */
|
|
135
|
+
getFilePath(): string {
|
|
136
|
+
return this.filePath;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Get well-known CoAIA variables that are currently set. */
|
|
140
|
+
getKnownVars(): Partial<Record<KnownVar, string>> {
|
|
141
|
+
const result: Partial<Record<KnownVar, string>> = {};
|
|
142
|
+
for (const key of KNOWN_VARS) {
|
|
143
|
+
const val = this.get(key);
|
|
144
|
+
if (val !== undefined) {
|
|
145
|
+
result[key] = val;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Create a default EnvironmentManager for the current directory. */
|
|
153
|
+
export function createEnvironment(dir?: string): EnvironmentManager {
|
|
154
|
+
const mgr = new EnvironmentManager(dir);
|
|
155
|
+
mgr.init();
|
|
156
|
+
return mgr;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Find and load environment from cwd or home. */
|
|
160
|
+
export function findEnvironment(): EnvironmentManager {
|
|
161
|
+
const cwdPath = resolve(process.cwd(), ENV_FILENAME);
|
|
162
|
+
if (existsSync(cwdPath)) {
|
|
163
|
+
return createEnvironment(process.cwd());
|
|
164
|
+
}
|
|
165
|
+
const homePath = join(homedir(), ENV_FILENAME);
|
|
166
|
+
if (existsSync(homePath)) {
|
|
167
|
+
return createEnvironment(homedir());
|
|
168
|
+
}
|
|
169
|
+
// Return empty manager for cwd
|
|
170
|
+
return new EnvironmentManager(process.cwd());
|
|
171
|
+
}
|
package/src/github.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// coaiajs/src/github.ts — GitHub module
|
|
2
|
+
// Parity with coaiapy's cogh.py using @octokit/rest
|
|
3
|
+
|
|
4
|
+
import { Octokit } from '@octokit/rest';
|
|
5
|
+
import { getConfig } from './config.js';
|
|
6
|
+
|
|
7
|
+
let _client: Octokit | null = null;
|
|
8
|
+
|
|
9
|
+
function getOctokit(): Octokit {
|
|
10
|
+
if (!_client) {
|
|
11
|
+
const cfg = getConfig();
|
|
12
|
+
_client = new Octokit({
|
|
13
|
+
auth: cfg.github?.token ?? process.env['GITHUB_TOKEN'] ?? process.env['GH_TOKEN'],
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return _client;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Reset client (testing). */
|
|
20
|
+
export function resetClient(): void {
|
|
21
|
+
_client = null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Issue {
|
|
25
|
+
number: number;
|
|
26
|
+
title: string;
|
|
27
|
+
state: string;
|
|
28
|
+
body: string | null;
|
|
29
|
+
labels: Array<{ name: string }>;
|
|
30
|
+
assignees: Array<{ login: string }>;
|
|
31
|
+
created_at: string;
|
|
32
|
+
updated_at: string;
|
|
33
|
+
html_url: string;
|
|
34
|
+
user: { login: string } | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface IssueComment {
|
|
38
|
+
id: number;
|
|
39
|
+
body: string;
|
|
40
|
+
user: { login: string } | null;
|
|
41
|
+
created_at: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* List issues for a repository.
|
|
46
|
+
* Parity with coaiapy's cogh list_issues.
|
|
47
|
+
*/
|
|
48
|
+
export async function listIssues(
|
|
49
|
+
owner: string,
|
|
50
|
+
repo: string,
|
|
51
|
+
options?: {
|
|
52
|
+
state?: 'open' | 'closed' | 'all';
|
|
53
|
+
labels?: string;
|
|
54
|
+
per_page?: number;
|
|
55
|
+
page?: number;
|
|
56
|
+
},
|
|
57
|
+
): Promise<Issue[]> {
|
|
58
|
+
const client = getOctokit();
|
|
59
|
+
|
|
60
|
+
const response = await client.issues.listForRepo({
|
|
61
|
+
owner,
|
|
62
|
+
repo,
|
|
63
|
+
state: options?.state ?? 'open',
|
|
64
|
+
labels: options?.labels,
|
|
65
|
+
per_page: options?.per_page ?? 30,
|
|
66
|
+
page: options?.page ?? 1,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return response.data.map(mapIssue);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get a single issue by number.
|
|
74
|
+
*/
|
|
75
|
+
export async function getIssue(
|
|
76
|
+
owner: string,
|
|
77
|
+
repo: string,
|
|
78
|
+
issueNumber: number,
|
|
79
|
+
): Promise<Issue> {
|
|
80
|
+
const client = getOctokit();
|
|
81
|
+
|
|
82
|
+
const response = await client.issues.get({
|
|
83
|
+
owner,
|
|
84
|
+
repo,
|
|
85
|
+
issue_number: issueNumber,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return mapIssue(response.data);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get comments for an issue.
|
|
93
|
+
*/
|
|
94
|
+
export async function getIssueComments(
|
|
95
|
+
owner: string,
|
|
96
|
+
repo: string,
|
|
97
|
+
issueNumber: number,
|
|
98
|
+
): Promise<IssueComment[]> {
|
|
99
|
+
const client = getOctokit();
|
|
100
|
+
|
|
101
|
+
const response = await client.issues.listComments({
|
|
102
|
+
owner,
|
|
103
|
+
repo,
|
|
104
|
+
issue_number: issueNumber,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return response.data.map((c) => ({
|
|
108
|
+
id: c.id,
|
|
109
|
+
body: c.body ?? '',
|
|
110
|
+
user: c.user ? { login: c.user.login } : null,
|
|
111
|
+
created_at: c.created_at,
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function mapIssue(data: Record<string, unknown>): Issue {
|
|
116
|
+
const d = data as {
|
|
117
|
+
number: number;
|
|
118
|
+
title: string;
|
|
119
|
+
state: string;
|
|
120
|
+
body: string | null;
|
|
121
|
+
labels: Array<{ name?: string } | string>;
|
|
122
|
+
assignees: Array<{ login: string }> | null;
|
|
123
|
+
created_at: string;
|
|
124
|
+
updated_at: string;
|
|
125
|
+
html_url: string;
|
|
126
|
+
user: { login: string } | null;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
number: d.number,
|
|
131
|
+
title: d.title,
|
|
132
|
+
state: d.state,
|
|
133
|
+
body: d.body,
|
|
134
|
+
labels: (d.labels ?? []).map((l) =>
|
|
135
|
+
typeof l === 'string' ? { name: l } : { name: l.name ?? '' },
|
|
136
|
+
),
|
|
137
|
+
assignees: (d.assignees ?? []).map((a) => ({ login: a.login })),
|
|
138
|
+
created_at: d.created_at,
|
|
139
|
+
updated_at: d.updated_at,
|
|
140
|
+
html_url: d.html_url,
|
|
141
|
+
user: d.user ? { login: d.user.login } : null,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// coaiajs/src/langfuse/client.ts — Langfuse REST client
|
|
2
|
+
// Parity with cofuse.py's direct REST approach (no SDK dependency for API calls)
|
|
3
|
+
|
|
4
|
+
import { getConfig } from '../config.js';
|
|
5
|
+
|
|
6
|
+
export interface IngestionEvent {
|
|
7
|
+
id: string;
|
|
8
|
+
timestamp: string;
|
|
9
|
+
type: 'trace-create' | 'observation-create' | 'score-create' | 'sdk-log';
|
|
10
|
+
body: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface LangfuseClientConfig {
|
|
14
|
+
publicKey?: string;
|
|
15
|
+
secretKey?: string;
|
|
16
|
+
baseUrl?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class LangfuseClient {
|
|
20
|
+
private publicKey: string;
|
|
21
|
+
private secretKey: string;
|
|
22
|
+
private baseUrl: string;
|
|
23
|
+
|
|
24
|
+
constructor(config?: LangfuseClientConfig) {
|
|
25
|
+
const appConfig = getConfig();
|
|
26
|
+
this.publicKey = config?.publicKey ?? appConfig.langfuse?.publicKey ?? '';
|
|
27
|
+
this.secretKey = config?.secretKey ?? appConfig.langfuse?.secretKey ?? '';
|
|
28
|
+
this.baseUrl = (config?.baseUrl ?? appConfig.langfuse?.baseUrl ?? 'https://cloud.langfuse.com').replace(/\/+$/, '');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private getAuthHeader(): string {
|
|
32
|
+
const credentials = Buffer.from(`${this.publicKey}:${this.secretKey}`).toString('base64');
|
|
33
|
+
return `Basic ${credentials}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async request<T = unknown>(method: string, path: string, body?: unknown): Promise<T> {
|
|
37
|
+
const url = `${this.baseUrl}${path}`;
|
|
38
|
+
const headers: Record<string, string> = {
|
|
39
|
+
'Authorization': this.getAuthHeader(),
|
|
40
|
+
'Content-Type': 'application/json',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const init: RequestInit = { method, headers };
|
|
44
|
+
if (body !== undefined) {
|
|
45
|
+
init.body = JSON.stringify(body);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const response = await fetch(url, init);
|
|
49
|
+
const text = await response.text();
|
|
50
|
+
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
let detail: string;
|
|
53
|
+
try {
|
|
54
|
+
detail = JSON.stringify(JSON.parse(text), null, 2);
|
|
55
|
+
} catch {
|
|
56
|
+
detail = text;
|
|
57
|
+
}
|
|
58
|
+
throw new LangfuseApiError(
|
|
59
|
+
`Langfuse API error ${response.status}: ${detail}`,
|
|
60
|
+
response.status,
|
|
61
|
+
detail,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!text) return undefined as T;
|
|
66
|
+
return JSON.parse(text) as T;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async ingest(events: IngestionEvent[]): Promise<void> {
|
|
70
|
+
await this.request('POST', '/api/public/ingestion', { batch: events });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getBaseUrl(): string {
|
|
74
|
+
return this.baseUrl;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class LangfuseApiError extends Error {
|
|
79
|
+
constructor(
|
|
80
|
+
message: string,
|
|
81
|
+
public readonly statusCode: number,
|
|
82
|
+
public readonly detail: string,
|
|
83
|
+
) {
|
|
84
|
+
super(message);
|
|
85
|
+
this.name = 'LangfuseApiError';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Singleton with lazy init
|
|
90
|
+
let _client: LangfuseClient | null = null;
|
|
91
|
+
|
|
92
|
+
export function getClient(config?: LangfuseClientConfig): LangfuseClient {
|
|
93
|
+
if (!_client) {
|
|
94
|
+
_client = new LangfuseClient(config);
|
|
95
|
+
}
|
|
96
|
+
return _client;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function resetClient(): void {
|
|
100
|
+
_client = null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function nowISO(): string {
|
|
104
|
+
return new Date().toISOString();
|
|
105
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// coaiajs/src/langfuse/comments.ts — Comment operations
|
|
2
|
+
// Port of cofuse.py comment functions
|
|
3
|
+
|
|
4
|
+
import { getClient } from './client.js';
|
|
5
|
+
|
|
6
|
+
export interface CommentFilters {
|
|
7
|
+
objectType?: string;
|
|
8
|
+
objectId?: string;
|
|
9
|
+
authorUserId?: string;
|
|
10
|
+
page?: number;
|
|
11
|
+
limit?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function listComments(filters: CommentFilters): Promise<string> {
|
|
15
|
+
const client = getClient();
|
|
16
|
+
const params = new URLSearchParams();
|
|
17
|
+
|
|
18
|
+
if (filters.objectType) params.set('objectType', filters.objectType);
|
|
19
|
+
if (filters.objectId) params.set('objectId', filters.objectId);
|
|
20
|
+
if (filters.authorUserId) params.set('authorUserId', filters.authorUserId);
|
|
21
|
+
params.set('page', String(filters.page ?? 1));
|
|
22
|
+
params.set('limit', String(filters.limit ?? 50));
|
|
23
|
+
|
|
24
|
+
const qs = params.toString();
|
|
25
|
+
const result = await client.request<unknown>('GET', `/api/public/comments?${qs}`);
|
|
26
|
+
return JSON.stringify(result, null, 2);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function getComment(commentId: string): Promise<string> {
|
|
30
|
+
const client = getClient();
|
|
31
|
+
const result = await client.request<unknown>('GET', `/api/public/comments/${commentId}`);
|
|
32
|
+
return JSON.stringify(result, null, 2);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function createComment(params: {
|
|
36
|
+
text: string;
|
|
37
|
+
objectType: string;
|
|
38
|
+
objectId: string;
|
|
39
|
+
authorUserId?: string;
|
|
40
|
+
}): Promise<string> {
|
|
41
|
+
const client = getClient();
|
|
42
|
+
const data: Record<string, unknown> = {
|
|
43
|
+
content: params.text,
|
|
44
|
+
objectType: params.objectType,
|
|
45
|
+
objectId: params.objectId,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
if (params.authorUserId) data.authorUserId = params.authorUserId;
|
|
49
|
+
|
|
50
|
+
const result = await client.request<unknown>('POST', '/api/public/comments', data);
|
|
51
|
+
return JSON.stringify(result, null, 2);
|
|
52
|
+
}
|