@polpo-ai/llm 0.5.2

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/query.js ADDED
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Query functions — wrappers around AI SDK generateText/streamText
3
+ * with cooldown integration and provider-level failover.
4
+ */
5
+ import { generateText, streamText } from "ai";
6
+ import { resolveModel, parseModelSpec } from "./model-resolver.js";
7
+ import { mapReasoningToProviderOptions } from "./provider-factory.js";
8
+ import { isProviderInCooldown, markProviderCooldown, clearProviderCooldown, classifyProviderError, } from "./cooldown.js";
9
+ /**
10
+ * Handle billing disable for a provider.
11
+ * OAuth profile store removed — this is now a no-op placeholder.
12
+ * Provider-level cooldown (markProviderCooldown) still applies.
13
+ */
14
+ async function handleBillingDisable(_provider) {
15
+ // No-op: OAuth profile billing tracking removed.
16
+ // Provider-level cooldown is handled by markProviderCooldown() at the call site.
17
+ }
18
+ /**
19
+ * Simple prompt -> text completion using AI SDK generateText.
20
+ * Integrates with the cooldown system: marks provider cooldown on classified errors,
21
+ * clears cooldown on success. Uses async API key resolution (includes OAuth profiles).
22
+ */
23
+ export async function queryText(prompt, model, reasoning) {
24
+ const m = resolveModel(model);
25
+ const provider = m.provider;
26
+ try {
27
+ const providerOptions = mapReasoningToProviderOptions(provider, reasoning, m.maxTokens);
28
+ const opts = {
29
+ model: m.aiModel,
30
+ prompt,
31
+ maxOutputTokens: m.maxTokens,
32
+ };
33
+ if (providerOptions)
34
+ opts.providerOptions = providerOptions;
35
+ const response = await generateText(opts);
36
+ const text = response.text.trim();
37
+ // Success — clear any cooldown for this provider
38
+ clearProviderCooldown(provider);
39
+ return {
40
+ text,
41
+ usage: response.usage,
42
+ model: m,
43
+ };
44
+ }
45
+ catch (err) {
46
+ // Classify and potentially cooldown the provider
47
+ const classified = classifyProviderError(err);
48
+ if (classified.reason === "billing") {
49
+ markProviderCooldown(provider, classified.reason);
50
+ handleBillingDisable(provider);
51
+ }
52
+ else if (classified.shouldCooldown) {
53
+ markProviderCooldown(provider, classified.reason);
54
+ }
55
+ throw err;
56
+ }
57
+ }
58
+ /**
59
+ * Streaming prompt -> text with progress callback.
60
+ * Integrates with the cooldown system. Uses async API key resolution.
61
+ */
62
+ export async function queryStream(prompt, model, onProgress, reasoning) {
63
+ const m = resolveModel(model);
64
+ const provider = m.provider;
65
+ try {
66
+ const providerOptions = mapReasoningToProviderOptions(provider, reasoning, m.maxTokens);
67
+ const streamOpts = {
68
+ model: m.aiModel,
69
+ prompt,
70
+ maxOutputTokens: m.maxTokens,
71
+ };
72
+ if (providerOptions)
73
+ streamOpts.providerOptions = providerOptions;
74
+ const result = streamText(streamOpts);
75
+ for await (const chunk of result.textStream) {
76
+ if (onProgress) {
77
+ onProgress(chunk);
78
+ }
79
+ }
80
+ // Wait for the full result to get usage data
81
+ const text = (await result.text).trim();
82
+ const usage = await result.usage;
83
+ // Success — clear cooldown
84
+ clearProviderCooldown(provider);
85
+ return {
86
+ text,
87
+ usage,
88
+ model: m,
89
+ };
90
+ }
91
+ catch (err) {
92
+ const classified = classifyProviderError(err);
93
+ if (classified.reason === "billing") {
94
+ markProviderCooldown(provider, classified.reason);
95
+ handleBillingDisable(provider);
96
+ }
97
+ else if (classified.shouldCooldown) {
98
+ markProviderCooldown(provider, classified.reason);
99
+ }
100
+ throw err;
101
+ }
102
+ }
103
+ /**
104
+ * Query with model fallback chain — tries primary model, then fallbacks.
105
+ * On provider-level errors, marks cooldown and tries next model.
106
+ */
107
+ export async function queryTextWithFallback(prompt, modelConfig) {
108
+ if (!modelConfig.primary) {
109
+ throw new Error("No primary model configured. Run 'polpo setup' or set POLPO_MODEL env var.");
110
+ }
111
+ const specs = [modelConfig.primary, ...(modelConfig.fallbacks || [])];
112
+ let lastError;
113
+ for (const spec of specs) {
114
+ const { provider } = parseModelSpec(spec);
115
+ // Skip providers in cooldown
116
+ if (isProviderInCooldown(provider))
117
+ continue;
118
+ try {
119
+ const result = await queryText(prompt, spec);
120
+ clearProviderCooldown(provider);
121
+ return { ...result, usedSpec: spec };
122
+ }
123
+ catch (err) {
124
+ lastError = err;
125
+ const classified = classifyProviderError(err);
126
+ if (classified.shouldCooldown) {
127
+ markProviderCooldown(provider, classified.reason);
128
+ }
129
+ if (!classified.shouldFailover) {
130
+ throw err; // Non-retryable error — don't try other providers
131
+ }
132
+ // Continue to next fallback
133
+ }
134
+ }
135
+ throw lastError ?? new Error("All model providers failed or are in cooldown");
136
+ }
137
+ //# sourceMappingURL=query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAA2B,MAAM,IAAI,CAAC;AAGvE,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,eAAe,CAAC;AAEvB;;;;GAIG;AACH,KAAK,UAAU,oBAAoB,CAAC,SAAiB;IACnD,iDAAiD;IACjD,iFAAiF;AACnF,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,MAAc,EACd,KAAc,EACd,SAA0B;IAE1B,MAAM,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;IAC9B,MAAM,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC;IAE5B,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,6BAA6B,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC;QAExF,MAAM,IAAI,GAAQ;YAChB,KAAK,EAAE,CAAC,CAAC,OAAO;YAChB,MAAM;YACN,eAAe,EAAE,CAAC,CAAC,SAAS;SAC7B,CAAC;QACF,IAAI,eAAe;YAAE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AAE,CAAC,CAAC,CAAC;IAEtE,IAAI,SAAkB,CAAC;IAEvB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,EAAE,QAAQ,EAAE,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QAE1C,6BAA6B;QAC7B,IAAI,oBAAoB,CAAC,QAAQ,CAAC;YAAE,SAAS;QAE7C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YAC7C,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YAChC,OAAO,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;QACvC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,SAAS,GAAG,GAAG,CAAC;YAChB,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,CAAC,CAAC;YAC9C,IAAI,UAAU,CAAC,cAAc,EAAE,CAAC;gBAC9B,oBAAoB,CAAC,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;YACpD,CAAC;YACD,IAAI,CAAC,UAAU,CAAC,cAAc,EAAE,CAAC;gBAC/B,MAAM,GAAG,CAAC,CAAC,kDAAkD;YAC/D,CAAC;YACD,4BAA4B;QAC9B,CAAC;IACH,CAAC;IAED,MAAM,SAAS,IAAI,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;AAChF,CAAC"}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@polpo-ai/llm",
3
+ "version": "0.5.2",
4
+ "description": "LLM abstraction for Polpo — multi-provider model resolution, streaming, cost tracking, and failover built on Vercel AI SDK + AI Gateway",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist/",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "dependencies": {
20
+ "@ai-sdk/openai": "^3.0.48",
21
+ "@polpo-ai/core": "0.5.2"
22
+ },
23
+ "peerDependencies": {
24
+ "ai": ">=6.0.0",
25
+ "@ai-sdk/gateway": ">=3.0.0"
26
+ },
27
+ "license": "MIT",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/lumea-labs/polpo.git",
31
+ "directory": "packages/llm"
32
+ },
33
+ "homepage": "https://github.com/lumea-labs/polpo/tree/main/packages/llm",
34
+ "bugs": {
35
+ "url": "https://github.com/lumea-labs/polpo/issues"
36
+ },
37
+ "keywords": [
38
+ "polpo",
39
+ "llm",
40
+ "ai-sdk",
41
+ "ai-gateway",
42
+ "multi-provider",
43
+ "model-resolution"
44
+ ],
45
+ "author": "Lumea Labs",
46
+ "engines": {
47
+ "node": ">=18.0.0"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "scripts": {
53
+ "build": "tsc"
54
+ }
55
+ }