@cogenta/agents 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/dist/agents/define.d.ts +9 -0
- package/dist/agents/define.d.ts.map +1 -0
- package/dist/agents/define.js +25 -0
- package/dist/agents/define.js.map +1 -0
- package/dist/agents/registry.d.ts +29 -0
- package/dist/agents/registry.d.ts.map +1 -0
- package/dist/agents/registry.js +50 -0
- package/dist/agents/registry.js.map +1 -0
- package/dist/agents/types.d.ts +36 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/agents/types.js +2 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/audit/types.d.ts +23 -0
- package/dist/audit/types.d.ts.map +1 -0
- package/dist/audit/types.js +2 -0
- package/dist/audit/types.js.map +1 -0
- package/dist/audit/with-audit.d.ts +34 -0
- package/dist/audit/with-audit.d.ts.map +1 -0
- package/dist/audit/with-audit.js +65 -0
- package/dist/audit/with-audit.js.map +1 -0
- package/dist/autonomy/approval-queue.d.ts +13 -0
- package/dist/autonomy/approval-queue.d.ts.map +1 -0
- package/dist/autonomy/approval-queue.js +56 -0
- package/dist/autonomy/approval-queue.js.map +1 -0
- package/dist/autonomy/types.d.ts +36 -0
- package/dist/autonomy/types.d.ts.map +1 -0
- package/dist/autonomy/types.js +2 -0
- package/dist/autonomy/types.js.map +1 -0
- package/dist/autonomy/with-autonomy.d.ts +25 -0
- package/dist/autonomy/with-autonomy.d.ts.map +1 -0
- package/dist/autonomy/with-autonomy.js +77 -0
- package/dist/autonomy/with-autonomy.js.map +1 -0
- package/dist/budget/kill-switch.d.ts +14 -0
- package/dist/budget/kill-switch.d.ts.map +1 -0
- package/dist/budget/kill-switch.js +20 -0
- package/dist/budget/kill-switch.js.map +1 -0
- package/dist/budget/tracker.d.ts +15 -0
- package/dist/budget/tracker.d.ts.map +1 -0
- package/dist/budget/tracker.js +68 -0
- package/dist/budget/tracker.js.map +1 -0
- package/dist/budget/types.d.ts +35 -0
- package/dist/budget/types.d.ts.map +1 -0
- package/dist/budget/types.js +2 -0
- package/dist/budget/types.js.map +1 -0
- package/dist/eval/assert-threshold.d.ts +15 -0
- package/dist/eval/assert-threshold.d.ts.map +1 -0
- package/dist/eval/assert-threshold.js +27 -0
- package/dist/eval/assert-threshold.js.map +1 -0
- package/dist/eval/compare-prompt-versions.d.ts +17 -0
- package/dist/eval/compare-prompt-versions.d.ts.map +1 -0
- package/dist/eval/compare-prompt-versions.js +20 -0
- package/dist/eval/compare-prompt-versions.js.map +1 -0
- package/dist/eval/run-suite.d.ts +4 -0
- package/dist/eval/run-suite.d.ts.map +1 -0
- package/dist/eval/run-suite.js +18 -0
- package/dist/eval/run-suite.js.map +1 -0
- package/dist/eval/scorers.d.ts +13 -0
- package/dist/eval/scorers.d.ts.map +1 -0
- package/dist/eval/scorers.js +33 -0
- package/dist/eval/scorers.js.map +1 -0
- package/dist/eval/types.d.ts +27 -0
- package/dist/eval/types.d.ts.map +1 -0
- package/dist/eval/types.js +2 -0
- package/dist/eval/types.js.map +1 -0
- package/dist/identity/constitution.d.ts +17 -0
- package/dist/identity/constitution.d.ts.map +1 -0
- package/dist/identity/constitution.js +33 -0
- package/dist/identity/constitution.js.map +1 -0
- package/dist/identity/context.d.ts +49 -0
- package/dist/identity/context.d.ts.map +1 -0
- package/dist/identity/context.js +67 -0
- package/dist/identity/context.js.map +1 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/file-store.d.ts +6 -0
- package/dist/memory/file-store.d.ts.map +1 -0
- package/dist/memory/file-store.js +94 -0
- package/dist/memory/file-store.js.map +1 -0
- package/dist/memory/from-approval.d.ts +20 -0
- package/dist/memory/from-approval.d.ts.map +1 -0
- package/dist/memory/from-approval.js +38 -0
- package/dist/memory/from-approval.js.map +1 -0
- package/dist/memory/memory-store.d.ts +4 -0
- package/dist/memory/memory-store.d.ts.map +1 -0
- package/dist/memory/memory-store.js +55 -0
- package/dist/memory/memory-store.js.map +1 -0
- package/dist/memory/types.d.ts +67 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +2 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/privacy/assert-provider-allowed.d.ts +10 -0
- package/dist/privacy/assert-provider-allowed.d.ts.map +1 -0
- package/dist/privacy/assert-provider-allowed.js +20 -0
- package/dist/privacy/assert-provider-allowed.js.map +1 -0
- package/dist/privacy/redact-fields.d.ts +11 -0
- package/dist/privacy/redact-fields.d.ts.map +1 -0
- package/dist/privacy/redact-fields.js +26 -0
- package/dist/privacy/redact-fields.js.map +1 -0
- package/dist/privacy/redact-text.d.ts +4 -0
- package/dist/privacy/redact-text.d.ts.map +1 -0
- package/dist/privacy/redact-text.js +26 -0
- package/dist/privacy/redact-text.js.map +1 -0
- package/dist/privacy/types.d.ts +23 -0
- package/dist/privacy/types.d.ts.map +1 -0
- package/dist/privacy/types.js +2 -0
- package/dist/privacy/types.js.map +1 -0
- package/dist/providers/anthropic.d.ts +57 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +130 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/google.d.ts +63 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +144 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/openai.d.ts +56 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +143 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/registry.d.ts +31 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +35 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/types.d.ts +63 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +10 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/rag/chunking/chunk-document.d.ts +13 -0
- package/dist/rag/chunking/chunk-document.d.ts.map +1 -0
- package/dist/rag/chunking/chunk-document.js +51 -0
- package/dist/rag/chunking/chunk-document.js.map +1 -0
- package/dist/rag/chunking/incremental.d.ts +19 -0
- package/dist/rag/chunking/incremental.d.ts.map +1 -0
- package/dist/rag/chunking/incremental.js +24 -0
- package/dist/rag/chunking/incremental.js.map +1 -0
- package/dist/rag/chunking/types.d.ts +34 -0
- package/dist/rag/chunking/types.d.ts.map +1 -0
- package/dist/rag/chunking/types.js +2 -0
- package/dist/rag/chunking/types.js.map +1 -0
- package/dist/rag/embeddings/hashing-provider.d.ts +22 -0
- package/dist/rag/embeddings/hashing-provider.d.ts.map +1 -0
- package/dist/rag/embeddings/hashing-provider.js +53 -0
- package/dist/rag/embeddings/hashing-provider.js.map +1 -0
- package/dist/rag/embeddings/types.d.ts +16 -0
- package/dist/rag/embeddings/types.d.ts.map +1 -0
- package/dist/rag/embeddings/types.js +2 -0
- package/dist/rag/embeddings/types.js.map +1 -0
- package/dist/rag/index/bm25.d.ts +4 -0
- package/dist/rag/index/bm25.d.ts.map +1 -0
- package/dist/rag/index/bm25.js +38 -0
- package/dist/rag/index/bm25.js.map +1 -0
- package/dist/rag/index/memory-index.d.ts +12 -0
- package/dist/rag/index/memory-index.d.ts.map +1 -0
- package/dist/rag/index/memory-index.js +44 -0
- package/dist/rag/index/memory-index.js.map +1 -0
- package/dist/rag/index/rrf.d.ts +9 -0
- package/dist/rag/index/rrf.d.ts.map +1 -0
- package/dist/rag/index/rrf.js +20 -0
- package/dist/rag/index/rrf.js.map +1 -0
- package/dist/rag/index/types.d.ts +48 -0
- package/dist/rag/index/types.d.ts.map +1 -0
- package/dist/rag/index/types.js +2 -0
- package/dist/rag/index/types.js.map +1 -0
- package/dist/rag/index/vector-rank.d.ts +4 -0
- package/dist/rag/index/vector-rank.d.ts.map +1 -0
- package/dist/rag/index/vector-rank.js +27 -0
- package/dist/rag/index/vector-rank.js.map +1 -0
- package/dist/reversibility/diff.d.ts +17 -0
- package/dist/reversibility/diff.d.ts.map +1 -0
- package/dist/reversibility/diff.js +39 -0
- package/dist/reversibility/diff.js.map +1 -0
- package/dist/reversibility/receipt-store.d.ts +9 -0
- package/dist/reversibility/receipt-store.d.ts.map +1 -0
- package/dist/reversibility/receipt-store.js +34 -0
- package/dist/reversibility/receipt-store.js.map +1 -0
- package/dist/reversibility/revert.d.ts +18 -0
- package/dist/reversibility/revert.d.ts.map +1 -0
- package/dist/reversibility/revert.js +38 -0
- package/dist/reversibility/revert.js.map +1 -0
- package/dist/reversibility/types.d.ts +25 -0
- package/dist/reversibility/types.d.ts.map +1 -0
- package/dist/reversibility/types.js +2 -0
- package/dist/reversibility/types.js.map +1 -0
- package/dist/reversibility/with-receipts.d.ts +19 -0
- package/dist/reversibility/with-receipts.d.ts.map +1 -0
- package/dist/reversibility/with-receipts.js +35 -0
- package/dist/reversibility/with-receipts.js.map +1 -0
- package/dist/runtime/loop.d.ts +11 -0
- package/dist/runtime/loop.d.ts.map +1 -0
- package/dist/runtime/loop.js +116 -0
- package/dist/runtime/loop.js.map +1 -0
- package/dist/runtime/repetition.d.ts +16 -0
- package/dist/runtime/repetition.d.ts.map +1 -0
- package/dist/runtime/repetition.js +38 -0
- package/dist/runtime/repetition.js.map +1 -0
- package/dist/runtime/retry.d.ts +22 -0
- package/dist/runtime/retry.d.ts.map +1 -0
- package/dist/runtime/retry.js +69 -0
- package/dist/runtime/retry.js.map +1 -0
- package/dist/runtime/types.d.ts +78 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +2 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/sandbox/types.d.ts +20 -0
- package/dist/sandbox/types.d.ts.map +1 -0
- package/dist/sandbox/types.js +2 -0
- package/dist/sandbox/types.js.map +1 -0
- package/dist/sandbox/with-sandbox.d.ts +26 -0
- package/dist/sandbox/with-sandbox.d.ts.map +1 -0
- package/dist/sandbox/with-sandbox.js +41 -0
- package/dist/sandbox/with-sandbox.js.map +1 -0
- package/dist/skills/file-store.d.ts +13 -0
- package/dist/skills/file-store.d.ts.map +1 -0
- package/dist/skills/file-store.js +73 -0
- package/dist/skills/file-store.js.map +1 -0
- package/dist/skills/frontmatter.d.ts +11 -0
- package/dist/skills/frontmatter.d.ts.map +1 -0
- package/dist/skills/frontmatter.js +46 -0
- package/dist/skills/frontmatter.js.map +1 -0
- package/dist/skills/types.d.ts +19 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +2 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/skin/generate.d.ts +22 -0
- package/dist/skin/generate.d.ts.map +1 -0
- package/dist/skin/generate.js +144 -0
- package/dist/skin/generate.js.map +1 -0
- package/dist/subagents/run-subagent.d.ts +10 -0
- package/dist/subagents/run-subagent.d.ts.map +1 -0
- package/dist/subagents/run-subagent.js +24 -0
- package/dist/subagents/run-subagent.js.map +1 -0
- package/dist/subagents/types.d.ts +13 -0
- package/dist/subagents/types.d.ts.map +1 -0
- package/dist/subagents/types.js +2 -0
- package/dist/subagents/types.js.map +1 -0
- package/dist/subagents/validate.d.ts +8 -0
- package/dist/subagents/validate.d.ts.map +1 -0
- package/dist/subagents/validate.js +32 -0
- package/dist/subagents/validate.js.map +1 -0
- package/dist/tools/core/agent-delegate.d.ts +34 -0
- package/dist/tools/core/agent-delegate.d.ts.map +1 -0
- package/dist/tools/core/agent-delegate.js +41 -0
- package/dist/tools/core/agent-delegate.js.map +1 -0
- package/dist/tools/core/content.d.ts +81 -0
- package/dist/tools/core/content.d.ts.map +1 -0
- package/dist/tools/core/content.js +121 -0
- package/dist/tools/core/content.js.map +1 -0
- package/dist/tools/core/http-fetch.d.ts +33 -0
- package/dist/tools/core/http-fetch.d.ts.map +1 -0
- package/dist/tools/core/http-fetch.js +63 -0
- package/dist/tools/core/http-fetch.js.map +1 -0
- package/dist/tools/core/media.d.ts +38 -0
- package/dist/tools/core/media.d.ts.map +1 -0
- package/dist/tools/core/media.js +93 -0
- package/dist/tools/core/media.js.map +1 -0
- package/dist/tools/core/site-config.d.ts +19 -0
- package/dist/tools/core/site-config.d.ts.map +1 -0
- package/dist/tools/core/site-config.js +37 -0
- package/dist/tools/core/site-config.js.map +1 -0
- package/dist/tools/define.d.ts +12 -0
- package/dist/tools/define.d.ts.map +1 -0
- package/dist/tools/define.js +28 -0
- package/dist/tools/define.js.map +1 -0
- package/dist/tools/manifest.d.ts +19 -0
- package/dist/tools/manifest.d.ts.map +1 -0
- package/dist/tools/manifest.js +70 -0
- package/dist/tools/manifest.js.map +1 -0
- package/dist/tools/registry.d.ts +8 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +20 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/types.d.ts +51 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/trace/capture.d.ts +19 -0
- package/dist/trace/capture.d.ts.map +1 -0
- package/dist/trace/capture.js +31 -0
- package/dist/trace/capture.js.map +1 -0
- package/dist/trace/file-store.d.ts +12 -0
- package/dist/trace/file-store.d.ts.map +1 -0
- package/dist/trace/file-store.js +78 -0
- package/dist/trace/file-store.js.map +1 -0
- package/dist/trace/memory-store.d.ts +9 -0
- package/dist/trace/memory-store.d.ts.map +1 -0
- package/dist/trace/memory-store.js +36 -0
- package/dist/trace/memory-store.js.map +1 -0
- package/dist/trace/types.d.ts +37 -0
- package/dist/trace/types.d.ts.map +1 -0
- package/dist/trace/types.js +2 -0
- package/dist/trace/types.js.map +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Order matters: a credit card number's digit groups must be checked before
|
|
3
|
+
* the looser phone pattern would otherwise swallow it. Pattern-based, not
|
|
4
|
+
* ML-based — deterministic and dependency-free (R9), matching "liste
|
|
5
|
+
* explicite des champs" in spirit: an explicit, auditable set of shapes,
|
|
6
|
+
* not a black box.
|
|
7
|
+
*/
|
|
8
|
+
const PATTERNS = [
|
|
9
|
+
{ kind: 'credit_card', regex: /\b\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{1,7}\b/g },
|
|
10
|
+
{ kind: 'email', regex: /[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}/g },
|
|
11
|
+
{ kind: 'ip_address', regex: /\b(?:\d{1,3}\.){3}\d{1,3}\b/g },
|
|
12
|
+
{ kind: 'phone', regex: /\+?\d[\d\s().-]{7,}\d/g },
|
|
13
|
+
];
|
|
14
|
+
/** Replaces every recognised PII shape with `[REDACTED:kind]`, in one pass per pattern kind, and reports what it found. */
|
|
15
|
+
export function redactText(text) {
|
|
16
|
+
let result = text;
|
|
17
|
+
const matches = [];
|
|
18
|
+
for (const { kind, regex } of PATTERNS) {
|
|
19
|
+
result = result.replace(regex, (match, offset) => {
|
|
20
|
+
matches.push({ kind, value: match, index: offset });
|
|
21
|
+
return `[REDACTED:${kind}]`;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return { text: result, matches };
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=redact-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redact-text.js","sourceRoot":"","sources":["../../src/privacy/redact-text.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,QAAQ,GAA0B;IACtC,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,4CAA4C,EAAE;IAC5E,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,gCAAgC,EAAE;IAC1D,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,8BAA8B,EAAE;IAC7D,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,wBAAwB,EAAE;CACnD,CAAA;AAED,2HAA2H;AAC3H,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,IAAI,MAAM,GAAG,IAAI,CAAA;IACjB,MAAM,OAAO,GAAe,EAAE,CAAA;IAE9B,KAAK,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,QAAQ,EAAE,CAAC;QACvC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,KAAa,EAAE,MAAc,EAAE,EAAE;YAC/D,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAA;YACnD,OAAO,aAAa,IAAI,GAAG,CAAA;QAC7B,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAA;AAClC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type PiiKind = 'email' | 'phone' | 'credit_card' | 'ip_address';
|
|
2
|
+
export interface PiiMatch {
|
|
3
|
+
readonly kind: PiiKind;
|
|
4
|
+
readonly value: string;
|
|
5
|
+
/** Offset in the text as it stood right before this match's own pattern pass — not stable across the whole `redactText` call once earlier passes have already replaced text. */
|
|
6
|
+
readonly index: number;
|
|
7
|
+
}
|
|
8
|
+
export interface RedactionResult {
|
|
9
|
+
readonly text: string;
|
|
10
|
+
readonly matches: readonly PiiMatch[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* "Redaction des données personnelles avant envoi au modèle, avec mode
|
|
14
|
+
* « rien ne sort »." `enabled: true` makes any provider not named in
|
|
15
|
+
* `localProviderNames` an error, not a fallback — a run configured this way
|
|
16
|
+
* must genuinely be unable to leak, not merely be discouraged from it.
|
|
17
|
+
*/
|
|
18
|
+
export interface NoDataLeavesPolicy {
|
|
19
|
+
readonly enabled: boolean;
|
|
20
|
+
/** Provider names treated as never leaving this machine (e.g. a locally-hosted client's `.name`). */
|
|
21
|
+
readonly localProviderNames: readonly string[];
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/privacy/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,aAAa,GAAG,YAAY,CAAA;AAEtE,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,gLAAgL;IAChL,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,SAAS,QAAQ,EAAE,CAAA;CACtC;AAED;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,qGAAqG;IACrG,QAAQ,CAAC,kBAAkB,EAAE,SAAS,MAAM,EAAE,CAAA;CAC/C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/privacy/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { ChatRequest, ChatResponse, ProviderClient } from './types.js';
|
|
2
|
+
type AnthropicContentBlock = {
|
|
3
|
+
readonly type: 'text';
|
|
4
|
+
readonly text: string;
|
|
5
|
+
} | {
|
|
6
|
+
readonly type: 'tool_use';
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly input: unknown;
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: 'tool_result';
|
|
12
|
+
readonly tool_use_id: string;
|
|
13
|
+
readonly content: string;
|
|
14
|
+
};
|
|
15
|
+
interface AnthropicMessage {
|
|
16
|
+
readonly role: 'user' | 'assistant';
|
|
17
|
+
readonly content: string | readonly AnthropicContentBlock[];
|
|
18
|
+
}
|
|
19
|
+
export interface AnthropicRequestBody {
|
|
20
|
+
readonly model: string;
|
|
21
|
+
readonly max_tokens: number;
|
|
22
|
+
readonly system?: string;
|
|
23
|
+
readonly messages: readonly AnthropicMessage[];
|
|
24
|
+
readonly tools?: readonly {
|
|
25
|
+
readonly name: string;
|
|
26
|
+
readonly description: string;
|
|
27
|
+
readonly input_schema: Readonly<Record<string, unknown>>;
|
|
28
|
+
}[];
|
|
29
|
+
readonly temperature?: number;
|
|
30
|
+
}
|
|
31
|
+
interface AnthropicResponseBody {
|
|
32
|
+
readonly content: readonly AnthropicContentBlock[];
|
|
33
|
+
readonly stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use';
|
|
34
|
+
readonly usage: {
|
|
35
|
+
readonly input_tokens: number;
|
|
36
|
+
readonly output_tokens: number;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Pure — no network. The whole reason this is separate from `createAnthropicClient` is to unit-test the mapping without a live call. */
|
|
40
|
+
export declare function buildAnthropicRequest(request: ChatRequest): AnthropicRequestBody;
|
|
41
|
+
/** Pure — no network. */
|
|
42
|
+
export declare function parseAnthropicResponse(body: AnthropicResponseBody): ChatResponse;
|
|
43
|
+
export interface AnthropicClientConfig {
|
|
44
|
+
readonly apiKey: string;
|
|
45
|
+
readonly model: string;
|
|
46
|
+
/** Overridable for tests — never for anything else. */
|
|
47
|
+
readonly baseUrl?: string;
|
|
48
|
+
readonly fetchImpl?: typeof fetch;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The API key is injected here, at the runtime boundary, from
|
|
52
|
+
* `COGENTA_ANTHROPIC_API_KEY` (or wherever the caller sources it) — never
|
|
53
|
+
* placed in a prompt or tool input (rule R7).
|
|
54
|
+
*/
|
|
55
|
+
export declare function createAnthropicClient(config: AnthropicClientConfig): ProviderClient;
|
|
56
|
+
export {};
|
|
57
|
+
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,KAA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EAAE,SAAS,qBAAqB,EAAE,CAAA;IAClD,QAAQ,CAAC,WAAW,EAAE,UAAU,GAAG,YAAY,GAAG,eAAe,GAAG,UAAU,CAAA;IAC9E,QAAQ,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAA;CAClF;AAwCD,yIAAyI;AACzI,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,WAAW,GAAG,oBAAoB,CAiBhF;AAED,yBAAyB;AACzB,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,qBAAqB,GAAG,YAAY,CAmBhF;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,uDAAuD;IACvD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAA;CAClC;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,qBAAqB,GAAG,cAAc,CA8CnF"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { CogentaError } from '@cogenta/core';
|
|
2
|
+
const API_VERSION = '2023-06-01';
|
|
3
|
+
const DEFAULT_BASE_URL = 'https://api.anthropic.com/v1/messages';
|
|
4
|
+
const STOP_REASON = {
|
|
5
|
+
end_turn: 'end_turn',
|
|
6
|
+
max_tokens: 'max_tokens',
|
|
7
|
+
stop_sequence: 'stop_sequence',
|
|
8
|
+
tool_use: 'tool_use',
|
|
9
|
+
};
|
|
10
|
+
function toAnthropicMessage(message) {
|
|
11
|
+
if (message.role === 'tool') {
|
|
12
|
+
if (message.toolCallId === undefined) {
|
|
13
|
+
throw new CogentaError({
|
|
14
|
+
code: 'PROVIDER_RESPONSE_INVALID',
|
|
15
|
+
message: 'A tool-role message is missing its toolCallId.',
|
|
16
|
+
hint: "Set toolCallId to the id the model's tool_use call carried.",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
role: 'user',
|
|
21
|
+
content: [
|
|
22
|
+
{ type: 'tool_result', tool_use_id: message.toolCallId, content: message.content ?? '' },
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const role = message.role === 'assistant' ? 'assistant' : 'user';
|
|
27
|
+
const blocks = [];
|
|
28
|
+
if (message.content !== undefined)
|
|
29
|
+
blocks.push({ type: 'text', text: message.content });
|
|
30
|
+
for (const call of message.toolCalls ?? []) {
|
|
31
|
+
blocks.push({ type: 'tool_use', id: call.id, name: call.name, input: call.input });
|
|
32
|
+
}
|
|
33
|
+
// A plain string is equivalent to a single text block and reads better in
|
|
34
|
+
// request logs — only messages that carry tool content need the array form.
|
|
35
|
+
if (blocks.length === 1 && blocks[0]?.type === 'text') {
|
|
36
|
+
return { role, content: blocks[0].text };
|
|
37
|
+
}
|
|
38
|
+
return { role, content: blocks };
|
|
39
|
+
}
|
|
40
|
+
/** Pure — no network. The whole reason this is separate from `createAnthropicClient` is to unit-test the mapping without a live call. */
|
|
41
|
+
export function buildAnthropicRequest(request) {
|
|
42
|
+
return {
|
|
43
|
+
model: request.model,
|
|
44
|
+
max_tokens: request.maxTokens,
|
|
45
|
+
...(request.system === undefined ? {} : { system: request.system }),
|
|
46
|
+
messages: request.messages.map(toAnthropicMessage),
|
|
47
|
+
...(request.tools === undefined
|
|
48
|
+
? {}
|
|
49
|
+
: {
|
|
50
|
+
tools: request.tools.map((tool) => ({
|
|
51
|
+
name: tool.name,
|
|
52
|
+
description: tool.description,
|
|
53
|
+
input_schema: tool.inputSchema,
|
|
54
|
+
})),
|
|
55
|
+
}),
|
|
56
|
+
...(request.temperature === undefined ? {} : { temperature: request.temperature }),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Pure — no network. */
|
|
60
|
+
export function parseAnthropicResponse(body) {
|
|
61
|
+
const textParts = [];
|
|
62
|
+
const toolCalls = [];
|
|
63
|
+
for (const block of body.content) {
|
|
64
|
+
if (block.type === 'text')
|
|
65
|
+
textParts.push(block.text);
|
|
66
|
+
else if (block.type === 'tool_use') {
|
|
67
|
+
toolCalls.push({
|
|
68
|
+
id: block.id,
|
|
69
|
+
name: block.name,
|
|
70
|
+
input: (block.input ?? {}),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
content: textParts.length === 0 ? null : textParts.join(''),
|
|
76
|
+
toolCalls,
|
|
77
|
+
stopReason: STOP_REASON[body.stop_reason],
|
|
78
|
+
usage: { inputTokens: body.usage.input_tokens, outputTokens: body.usage.output_tokens },
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The API key is injected here, at the runtime boundary, from
|
|
83
|
+
* `COGENTA_ANTHROPIC_API_KEY` (or wherever the caller sources it) — never
|
|
84
|
+
* placed in a prompt or tool input (rule R7).
|
|
85
|
+
*/
|
|
86
|
+
export function createAnthropicClient(config) {
|
|
87
|
+
const doFetch = config.fetchImpl ?? fetch;
|
|
88
|
+
const url = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
89
|
+
return {
|
|
90
|
+
name: 'anthropic',
|
|
91
|
+
model: config.model,
|
|
92
|
+
async chat(request, options) {
|
|
93
|
+
const response = await doFetch(url, {
|
|
94
|
+
method: 'POST',
|
|
95
|
+
headers: {
|
|
96
|
+
'content-type': 'application/json',
|
|
97
|
+
'x-api-key': config.apiKey,
|
|
98
|
+
'anthropic-version': API_VERSION,
|
|
99
|
+
},
|
|
100
|
+
body: JSON.stringify(buildAnthropicRequest(request)),
|
|
101
|
+
...(options?.signal === undefined ? {} : { signal: options.signal }),
|
|
102
|
+
}).catch((cause) => {
|
|
103
|
+
throw new CogentaError({
|
|
104
|
+
code: 'PROVIDER_REQUEST_FAILED',
|
|
105
|
+
message: 'The request to Anthropic could not be sent.',
|
|
106
|
+
hint: 'Check network connectivity and COGENTA_ANTHROPIC_API_KEY.',
|
|
107
|
+
cause,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
if (response.status === 429) {
|
|
111
|
+
throw new CogentaError({
|
|
112
|
+
code: 'PROVIDER_RATE_LIMITED',
|
|
113
|
+
message: 'Anthropic rate-limited this request.',
|
|
114
|
+
hint: 'Retry with backoff, or lower callsPerHour for this agent.',
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
throw new CogentaError({
|
|
119
|
+
code: 'PROVIDER_REQUEST_FAILED',
|
|
120
|
+
message: `Anthropic returned status ${response.status}.`,
|
|
121
|
+
hint: 'Check the request and COGENTA_ANTHROPIC_API_KEY.',
|
|
122
|
+
details: { status: response.status },
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const body = (await response.json());
|
|
126
|
+
return parseAnthropicResponse(body);
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=anthropic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAW5C,MAAM,WAAW,GAAG,YAAY,CAAA;AAChC,MAAM,gBAAgB,GAAG,uCAAuC,CAAA;AAoChE,MAAM,WAAW,GAA6D;IAC5E,QAAQ,EAAE,UAAU;IACpB,UAAU,EAAE,YAAY;IACxB,aAAa,EAAE,eAAe;IAC9B,QAAQ,EAAE,UAAU;CACrB,CAAA;AAED,SAAS,kBAAkB,CAAC,OAAoB;IAC9C,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;QAC5B,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YACrC,MAAM,IAAI,YAAY,CAAC;gBACrB,IAAI,EAAE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;oBACrB,IAAI,EAAE,yBAAyB;oBAC/B,OAAO,EAAE,6CAA6C;oBACtD,IAAI,EAAE,2DAA2D;oBACjE,KAAK;iBACN,CAAC,CAAA;YACJ,CAAC,CAAC,CAAA;YAEF,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;gBAC5B,MAAM,IAAI,YAAY,CAAC;oBACrB,IAAI,EAAE,uBAAuB;oBAC7B,OAAO,EAAE,sCAAsC;oBAC/C,IAAI,EAAE,2DAA2D;iBAClE,CAAC,CAAA;YACJ,CAAC;YACD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,IAAI,YAAY,CAAC;oBACrB,IAAI,EAAE,yBAAyB;oBAC/B,OAAO,EAAE,6BAA6B,QAAQ,CAAC,MAAM,GAAG;oBACxD,IAAI,EAAE,kDAAkD;oBACxD,OAAO,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE;iBACrC,CAAC,CAAA;YACJ,CAAC;YAED,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA0B,CAAA;YAC7D,OAAO,sBAAsB,CAAC,IAAI,CAAC,CAAA;QACrC,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ChatRequest, ChatResponse, ProviderClient } from './types.js';
|
|
2
|
+
type GooglePart = {
|
|
3
|
+
readonly text: string;
|
|
4
|
+
} | {
|
|
5
|
+
readonly functionCall: {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly args: Readonly<Record<string, unknown>>;
|
|
8
|
+
};
|
|
9
|
+
} | {
|
|
10
|
+
readonly functionResponse: {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly response: Readonly<Record<string, unknown>>;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
interface GoogleContent {
|
|
16
|
+
readonly role: 'user' | 'model';
|
|
17
|
+
readonly parts: readonly GooglePart[];
|
|
18
|
+
}
|
|
19
|
+
export interface GoogleRequestBody {
|
|
20
|
+
readonly contents: readonly GoogleContent[];
|
|
21
|
+
readonly systemInstruction?: {
|
|
22
|
+
readonly parts: readonly [{
|
|
23
|
+
readonly text: string;
|
|
24
|
+
}];
|
|
25
|
+
};
|
|
26
|
+
readonly tools?: readonly {
|
|
27
|
+
readonly functionDeclarations: readonly {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly description: string;
|
|
30
|
+
readonly parameters: Readonly<Record<string, unknown>>;
|
|
31
|
+
}[];
|
|
32
|
+
}[];
|
|
33
|
+
readonly generationConfig: {
|
|
34
|
+
readonly maxOutputTokens: number;
|
|
35
|
+
readonly temperature?: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
interface GoogleResponseBody {
|
|
39
|
+
readonly candidates: readonly {
|
|
40
|
+
readonly content: {
|
|
41
|
+
readonly parts: readonly GooglePart[];
|
|
42
|
+
};
|
|
43
|
+
readonly finishReason: 'STOP' | 'MAX_TOKENS' | 'SAFETY' | 'RECITATION' | 'OTHER';
|
|
44
|
+
}[];
|
|
45
|
+
readonly usageMetadata: {
|
|
46
|
+
readonly promptTokenCount: number;
|
|
47
|
+
readonly candidatesTokenCount: number;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** Pure — no network. */
|
|
51
|
+
export declare function buildGoogleRequest(request: ChatRequest): GoogleRequestBody;
|
|
52
|
+
/** Pure — no network. */
|
|
53
|
+
export declare function parseGoogleResponse(body: GoogleResponseBody): ChatResponse;
|
|
54
|
+
export interface GoogleClientConfig {
|
|
55
|
+
readonly apiKey: string;
|
|
56
|
+
readonly model: string;
|
|
57
|
+
readonly baseUrl?: string;
|
|
58
|
+
readonly fetchImpl?: typeof fetch;
|
|
59
|
+
}
|
|
60
|
+
/** API key injected at the runtime boundary — never in a prompt or tool input (rule R7). */
|
|
61
|
+
export declare function createGoogleClient(config: GoogleClientConfig): ProviderClient;
|
|
62
|
+
export {};
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L = 'https://generativelanguage.googleapis.com/v1beta/models';
|
|
3
|
+
const STOP_REASON = {
|
|
4
|
+
STOP: 'end_turn',
|
|
5
|
+
MAX_TOKENS: 'max_tokens',
|
|
6
|
+
SAFETY: 'stop_sequence',
|
|
7
|
+
RECITATION: 'stop_sequence',
|
|
8
|
+
OTHER: 'stop_sequence',
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Gemini has no `tool` role and no call-id concept at all — a function
|
|
12
|
+
* result is a `functionResponse` part inside a `user`-role turn, matched to
|
|
13
|
+
* its call by `name` alone. `toolCallId` still has to be threaded through the
|
|
14
|
+
* normalized shape (other vendors need it), so this adapter uses `toolName`
|
|
15
|
+
* when present and falls back to treating `toolCallId` as the name.
|
|
16
|
+
*/
|
|
17
|
+
function toGoogleContent(message) {
|
|
18
|
+
if (message.role === 'tool') {
|
|
19
|
+
const name = message.toolName ?? message.toolCallId;
|
|
20
|
+
if (name === undefined) {
|
|
21
|
+
throw new CogentaError({
|
|
22
|
+
code: 'PROVIDER_RESPONSE_INVALID',
|
|
23
|
+
message: 'A tool-role message is missing both toolName and toolCallId.',
|
|
24
|
+
hint: 'Gemini identifies a function result by name — set toolName.',
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
role: 'user',
|
|
29
|
+
parts: [{ functionResponse: { name, response: { result: message.content ?? '' } } }],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const role = message.role === 'assistant' ? 'model' : 'user';
|
|
33
|
+
const parts = [];
|
|
34
|
+
if (message.content !== undefined)
|
|
35
|
+
parts.push({ text: message.content });
|
|
36
|
+
for (const call of message.toolCalls ?? []) {
|
|
37
|
+
parts.push({ functionCall: { name: call.name, args: call.input } });
|
|
38
|
+
}
|
|
39
|
+
return { role, parts };
|
|
40
|
+
}
|
|
41
|
+
/** Pure — no network. */
|
|
42
|
+
export function buildGoogleRequest(request) {
|
|
43
|
+
return {
|
|
44
|
+
contents: request.messages.map(toGoogleContent),
|
|
45
|
+
...(request.system === undefined
|
|
46
|
+
? {}
|
|
47
|
+
: { systemInstruction: { parts: [{ text: request.system }] } }),
|
|
48
|
+
...(request.tools === undefined
|
|
49
|
+
? {}
|
|
50
|
+
: {
|
|
51
|
+
tools: [
|
|
52
|
+
{
|
|
53
|
+
functionDeclarations: request.tools.map((tool) => ({
|
|
54
|
+
name: tool.name,
|
|
55
|
+
description: tool.description,
|
|
56
|
+
parameters: tool.inputSchema,
|
|
57
|
+
})),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
}),
|
|
61
|
+
generationConfig: {
|
|
62
|
+
maxOutputTokens: request.maxTokens,
|
|
63
|
+
...(request.temperature === undefined ? {} : { temperature: request.temperature }),
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** Pure — no network. */
|
|
68
|
+
export function parseGoogleResponse(body) {
|
|
69
|
+
const candidate = body.candidates[0];
|
|
70
|
+
if (candidate === undefined) {
|
|
71
|
+
throw new CogentaError({
|
|
72
|
+
code: 'PROVIDER_RESPONSE_INVALID',
|
|
73
|
+
message: 'Google returned no candidates.',
|
|
74
|
+
hint: 'Retry the request; this is not something the caller can fix by itself.',
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const textParts = [];
|
|
78
|
+
const toolCalls = [];
|
|
79
|
+
for (const part of candidate.content.parts) {
|
|
80
|
+
if ('text' in part)
|
|
81
|
+
textParts.push(part.text);
|
|
82
|
+
else if ('functionCall' in part) {
|
|
83
|
+
// Synthesised, not vendor-issued: Gemini never hands back a call id,
|
|
84
|
+
// so the name is the closest stable handle the rest of the runtime has.
|
|
85
|
+
toolCalls.push({
|
|
86
|
+
id: part.functionCall.name,
|
|
87
|
+
name: part.functionCall.name,
|
|
88
|
+
input: part.functionCall.args,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
content: textParts.length === 0 ? null : textParts.join(''),
|
|
94
|
+
toolCalls,
|
|
95
|
+
stopReason: STOP_REASON[candidate.finishReason],
|
|
96
|
+
usage: {
|
|
97
|
+
inputTokens: body.usageMetadata.promptTokenCount,
|
|
98
|
+
outputTokens: body.usageMetadata.candidatesTokenCount,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** API key injected at the runtime boundary — never in a prompt or tool input (rule R7). */
|
|
103
|
+
export function createGoogleClient(config) {
|
|
104
|
+
const doFetch = config.fetchImpl ?? fetch;
|
|
105
|
+
const base = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
106
|
+
const url = `${base}/${config.model}:generateContent?key=${config.apiKey}`;
|
|
107
|
+
return {
|
|
108
|
+
name: 'google',
|
|
109
|
+
model: config.model,
|
|
110
|
+
async chat(request, options) {
|
|
111
|
+
const response = await doFetch(url, {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
headers: { 'content-type': 'application/json' },
|
|
114
|
+
body: JSON.stringify(buildGoogleRequest(request)),
|
|
115
|
+
...(options?.signal === undefined ? {} : { signal: options.signal }),
|
|
116
|
+
}).catch((cause) => {
|
|
117
|
+
throw new CogentaError({
|
|
118
|
+
code: 'PROVIDER_REQUEST_FAILED',
|
|
119
|
+
message: 'The request to Google could not be sent.',
|
|
120
|
+
hint: 'Check network connectivity and COGENTA_GOOGLE_API_KEY.',
|
|
121
|
+
cause,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
if (response.status === 429) {
|
|
125
|
+
throw new CogentaError({
|
|
126
|
+
code: 'PROVIDER_RATE_LIMITED',
|
|
127
|
+
message: 'Google rate-limited this request.',
|
|
128
|
+
hint: 'Retry with backoff, or lower callsPerHour for this agent.',
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (!response.ok) {
|
|
132
|
+
throw new CogentaError({
|
|
133
|
+
code: 'PROVIDER_REQUEST_FAILED',
|
|
134
|
+
message: `Google returned status ${response.status}.`,
|
|
135
|
+
hint: 'Check the request and COGENTA_GOOGLE_API_KEY.',
|
|
136
|
+
details: { status: response.status },
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const body = (await response.json());
|
|
140
|
+
return parseGoogleResponse(body);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=google.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/providers/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAW5C,MAAM,gBAAgB,GAAG,yDAAyD,CAAA;AAiDlF,MAAM,WAAW,GAAiF;IAChG,IAAI,EAAE,UAAU;IAChB,UAAU,EAAE,YAAY;IACxB,MAAM,EAAE,eAAe;IACvB,UAAU,EAAE,eAAe;IAC3B,KAAK,EAAE,eAAe;CACvB,CAAA;AAED;;;;;;GAMG;AACH,SAAS,eAAe,CAAC,OAAoB;IAC3C,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;QAC5B,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAA;QACnD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,MAAM,IAAI,YAAY,CAAC;gBACrB,IAAI,EAAE,2BAA2B;gBACjC,OAAO,EAAE,8DAA8D;gBACvE,IAAI,EAAE,6DAA6D;aACpE,CAAC,CAAA;QACJ,CAAC;QACD,OAAO;YACL,IAAI,EAAE,MAAM;YACZ,KAAK,EAAE,CAAC,EAAE,gBAAgB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC;SACrF,CAAA;IACH,CAAC;IAED,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAA;IAC5D,MAAM,KAA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E,CAAC,CAAA;YACJ,CAAC;YACD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,IAAI,YAAY,CAAC;oBACrB,IAAI,EAAE,yBAAyB;oBAC/B,OAAO,EAAE,0BAA0B,QAAQ,CAAC,MAAM,GAAG;oBACrD,IAAI,EAAE,+CAA+C;oBACrD,OAAO,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE;iBACrC,CAAC,CAAA;YACJ,CAAC;YAED,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAuB,CAAA;YAC1D,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAA;QAClC,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ChatRequest, ChatResponse, ProviderClient } from './types.js';
|
|
2
|
+
interface OpenAiToolCall {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly type: 'function';
|
|
5
|
+
readonly function: {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly arguments: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
interface OpenAiMessage {
|
|
11
|
+
readonly role: 'system' | 'user' | 'assistant' | 'tool';
|
|
12
|
+
readonly content: string | null;
|
|
13
|
+
readonly tool_calls?: readonly OpenAiToolCall[];
|
|
14
|
+
readonly tool_call_id?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface OpenAiRequestBody {
|
|
17
|
+
readonly model: string;
|
|
18
|
+
readonly max_tokens: number;
|
|
19
|
+
readonly messages: readonly OpenAiMessage[];
|
|
20
|
+
readonly tools?: readonly {
|
|
21
|
+
readonly type: 'function';
|
|
22
|
+
readonly function: {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly description: string;
|
|
25
|
+
readonly parameters: Readonly<Record<string, unknown>>;
|
|
26
|
+
};
|
|
27
|
+
}[];
|
|
28
|
+
readonly temperature?: number;
|
|
29
|
+
}
|
|
30
|
+
interface OpenAiResponseBody {
|
|
31
|
+
readonly choices: readonly {
|
|
32
|
+
readonly message: {
|
|
33
|
+
readonly content: string | null;
|
|
34
|
+
readonly tool_calls?: readonly OpenAiToolCall[];
|
|
35
|
+
};
|
|
36
|
+
readonly finish_reason: 'stop' | 'tool_calls' | 'length' | 'content_filter';
|
|
37
|
+
}[];
|
|
38
|
+
readonly usage: {
|
|
39
|
+
readonly prompt_tokens: number;
|
|
40
|
+
readonly completion_tokens: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Pure — no network. */
|
|
44
|
+
export declare function buildOpenAiRequest(request: ChatRequest): OpenAiRequestBody;
|
|
45
|
+
/** Pure — no network. */
|
|
46
|
+
export declare function parseOpenAiResponse(body: OpenAiResponseBody): ChatResponse;
|
|
47
|
+
export interface OpenAiClientConfig {
|
|
48
|
+
readonly apiKey: string;
|
|
49
|
+
readonly model: string;
|
|
50
|
+
readonly baseUrl?: string;
|
|
51
|
+
readonly fetchImpl?: typeof fetch;
|
|
52
|
+
}
|
|
53
|
+
/** API key injected at the runtime boundary — never in a prompt or tool input (rule R7). */
|
|
54
|
+
export declare function createOpenAiClient(config: OpenAiClientConfig): ProviderClient;
|
|
55
|
+
export {};
|
|
56
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,WAAW,EACX,YAAY,EACZ,cAAc,EAGf,MAAM,YAAY,CAAA;AAInB,UAAU,cAAc;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;IACzB,QAAQ,CAAC,QAAQ,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAA;CACzE;AA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