diff --git a/docs/hosted-pi.md b/docs/hosted-pi.md index 792f1c6b..011c8975 100644 --- a/docs/hosted-pi.md +++ b/docs/hosted-pi.md @@ -1,6 +1,6 @@ # Hosted Pi execution -Factorize bundles `@earendil-works/pi-agent-core` and `@earendil-works/pi-ai` at 1.5.2. The managed runtime runs in the Worker with Node compatibility, using the selected OpenAI Chat Completions adapter. Guest images do not install or launch managed agents. VM scripts retain their separate execution lifecycle. Worker identity, database, scheduler, and storage bindings are unchanged. +Factorize bundles `@earendil-works/pi-agent-core` and `@earendil-works/pi-ai` at 1.5.2. The managed runtime runs in the Worker with Node compatibility. Integrations configured with `https://api.openai.com/v1` use Pi's built-in OpenAI provider and the Responses API. Other OpenAI-compatible base URLs use Pi's Chat Completions adapter. Guest images do not install or launch managed agents. VM scripts retain their separate execution lifecycle. Worker identity, database, scheduler, and storage bindings are unchanged. A model integration stores an OpenAI-compatible provider base URL and optional API key, encrypted by the existing managed credential repository. The model ID belongs to Team defaults or the Trigger/run override. Model and VM catalogs are independent. Optional context/output limits control the framework request and compaction threshold. A keyless integration sends no Authorization header. Credentials are resolved anew for each scheduler turn; they never enter agent messages. diff --git a/packages/api/src/pi-provider.ts b/packages/api/src/pi-provider.ts index ce139c0d..733d92d1 100644 --- a/packages/api/src/pi-provider.ts +++ b/packages/api/src/pi-provider.ts @@ -1,6 +1,7 @@ import {z} from "zod"; import {createModels,createProvider} from "@earendil-works/pi-ai/models"; import {openAICompletionsApi} from "@earendil-works/pi-ai/api/openai-completions.lazy"; +import {openaiProvider} from "@earendil-works/pi-ai/providers/openai"; import type {Model} from "@earendil-works/pi-ai"; import {CredentialRepository} from "./postgres/credential-repository"; import type {Database} from "./postgres/database"; @@ -24,11 +25,53 @@ export async function runPiProvider(database:Database,env:Env,tenantId:string,ru if(revoked.rowCount || !config)throw new Error("The selected model integration is unavailable or needs a provider URL. Repair it before starting a new run."); return piProvider(config,modelId,effort); } -/** Only the selected OpenAI-compatible adapter is bundled; no global provider catalog. */ export function piProvider(config:ModelProviderConfiguration,modelId:string,effort?:string) { - const baseUrl=config.providerUrl.replace(/\/$/,""); - const model:Model<"openai-completions">={id:modelId,name:modelId,api:"openai-completions",provider:"factorize",baseUrl,reasoning:Boolean(effort),input:["text","image"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:config.contextWindow,maxTokens:config.maxTokens}; - const models=createModels(); - models.setProvider(createProvider({id:"factorize",name:"Factorize model integration",baseUrl,models:[model],auth:{apiKey:{name:"Integration credential",resolve:async()=>({auth:config.apiKey?{apiKey:config.apiKey}:{apiKey:"unused",headers:{Authorization:null}}})}},api:openAICompletionsApi()})); + const baseUrl = config.providerUrl.replace(/\/$/, ""); + const models = createModels(); + const auth = { + apiKey: { + name: "Integration credential", + resolve: async () => ({ + auth: config.apiKey ? { apiKey: config.apiKey } : { apiKey: "unused", headers: { Authorization: null } }, + }), + }, + }; + const configuredModel = { + id: modelId, + name: modelId, + baseUrl, + reasoning: Boolean(effort), + input: ["text", "image"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: config.contextWindow, + maxTokens: config.maxTokens, + } satisfies Omit, "api" | "provider">; + + if (new URL(baseUrl).href === "https://api.openai.com/v1") { + const provider = openaiProvider(); + const catalogModel = provider.getModels().find(model => model.id === modelId); + const model: Model<"openai-responses"> = { + ...configuredModel, + ...catalogModel, + api: "openai-responses", + provider: provider.id, + baseUrl, + contextWindow: config.contextWindow, + maxTokens: config.maxTokens, + }; + // Resolve only the saved integration credential, including for direct OpenAI runs. + models.setProvider({ ...provider, auth }); + return { models, model, config }; + } + + const model: Model<"openai-completions"> = { ...configuredModel, api: "openai-completions", provider: "factorize" }; + models.setProvider(createProvider({ + id: model.provider, + name: "Factorize model integration", + baseUrl, + models: [model], + auth, + api: openAICompletionsApi(), + })); return {models,model,config}; } diff --git a/scripts/hosted-pi-runtime.test.mjs b/scripts/hosted-pi-runtime.test.mjs index 6449b460..cf92af27 100644 --- a/scripts/hosted-pi-runtime.test.mjs +++ b/scripts/hosted-pi-runtime.test.mjs @@ -28,3 +28,58 @@ test("bundled Pi streams and executes a validated tool in the Factorize Worker r assert.equal(body.messages.find(message=>message.role==="toolResult").content[0].text,"8");assert.equal(body.messages.at(-1).content[0].text,"Worker Pi completed"); }finally{await worker.dispose();} }); + +test("direct OpenAI uses Pi's Responses provider with reasoning, tools, and the saved credential", async () => { + const source = `import {Agent} from '@earendil-works/pi-agent-core'; + import {piProvider} from './packages/api/src/pi-provider.ts'; + export default {async fetch() { + const {models, model} = piProvider({ + providerUrl: 'https://api.openai.com/v1/', apiKey: 'sk-test-fixture', contextWindow: 32000, maxTokens: 1000, + }, 'gpt-6-luna', 'high'); + const requests = []; + const modelFetch = async (input, init) => { + const request = new Request(input, init); + const body = await request.json(); + requests.push({url: request.url, authorization: request.headers.get('authorization'), body}); + const done = body.input.some(item => item.type === 'function_call_output'); + const item = done + ? {id: 'msg_fixture', type: 'message', role: 'assistant', content: [{type: 'output_text', text: 'Hello world', annotations: []}]} + : {id: 'fc_fixture', type: 'function_call', call_id: 'call_fixture', name: 'count', arguments: '{}'}; + const events = [ + {type: 'response.output_item.added', output_index: 0, item}, + {type: 'response.output_item.done', output_index: 0, item}, + {type: 'response.completed', response: {id: 'fixture', status: 'completed', output: [item]}}, + ]; + const stream = events.map(event => 'data: ' + JSON.stringify(event) + '\\n\\n').join(''); + return new Response(stream, {headers: {'Content-Type': 'text/event-stream'}}); + }; + const tools = [{name: 'count', label: 'Count', description: 'Count a value', parameters: {type: 'object', properties: {}}, + execute: async () => ({content: [{type: 'text', text: '1'}], details: {}})}]; + const agent = new Agent({initialState: {model, thinkingLevel: 'high', tools}, + streamFn: (model, context, options) => models.streamSimple(model, context, {...options, fetch: modelFetch})}); + await agent.prompt('Say hello world'); + return Response.json({requests, result: agent.state.messages.at(-1)}); + }};`; + const bundle = await build({stdin: {contents: source, resolveDir: process.cwd()}, bundle: true, platform: "browser", format: "esm", external: ["node:*"], write: false}); + const worker = new Miniflare({workers: [{config: {name: "hosted-pi", type: "worker", manifest: {mainModule: "hosted-pi.js", modules: {"hosted-pi.js": {type: "esm", contents: bundle.outputFiles[0].text}}}, compatibilityDate: "2026-09-10", compatibilityFlags: ["nodejs_compat"]}}]}); + try { + const response = await worker.dispatchFetch("https://hosted-pi.test"); + const {requests, result} = await response.json(); + assert.equal(requests.length, 2, result?.errorMessage); + const [request] = requests; + assert.equal(request.url, "https://api.openai.com/v1/responses"); + assert.equal(request.authorization, "Bearer sk-test-fixture"); + assert.equal(request.body.model, "gpt-6-luna"); + assert.equal(request.body.max_output_tokens, 1000); + const reasoningUpdate = request.body.input.find(item => item.type === "configuration_update"); + assert.equal(reasoningUpdate?.reasoning.effort ?? request.body.reasoning?.effort, "high"); + const declaredTools = request.body.tools ?? request.body.input.find(item => item.type === "additional_tools")?.tools; + assert.ok(declaredTools?.some(tool => tool.name === "count")); + const toolOutput = requests[1].body.input.find(item => item.type === "function_call_output"); + assert.equal(toolOutput?.output, "1"); + assert.equal(result.stopReason, "stop", result.errorMessage); + assert.equal(result.content.find(part => part.type === "text")?.text, "Hello world"); + } finally { + await worker.dispose(); + } +});