2import { email } from "https://esm.town/v/std/email";
3import { extractValInfo } from "https://esm.town/v/stevekrouse/extractValInfo";
4import { OpenAI } from "npm:openai";
5
6function stripHtmlBackticks(html: string): string {
9
10export default async function(e: Email) {
11 const openai = new OpenAI();
12 console.log(`from: ${e.from} to: ${e.to} subject: ${e.subject}, cc: ${e.cc}, bcc: ${e.bcc}`);
13
25 }
26
27 const summary = await openai.chat.completions.create({
28 messages: [
29 {
2import { Hono } from "npm:hono@4.4.12";
3// @ts-ignore
4import { OpenAI } from "https://esm.town/v/std/openai?v=4";
5
6// --- TownCar: The JIT Journey Engine v1.1 (AI Concierge Service) ---
485const app = new Hono();
486
487async function callOpenAI(c, messages, response_format = { type: "text" }) {
488 const openai = new OpenAI();
489 const completion = await openai.chat.completions.create({
490 model: "gpt-4o",
491 messages: messages,
494
495 if (!completion || !completion.choices || completion.choices.length === 0) {
496 console.error("Invalid response from OpenAI API:", JSON.stringify(completion, null, 2));
497 throw new Error("The AI service returned an invalid or empty response.");
498 }
505 try {
506 const messages = [{ role: "system", content: META_PROMPTS.ROUTE_PLANNER.replace("{{goal}}", goal) }];
507 const planJson = await callOpenAI(c, messages, { type: "json_object" });
508 return c.json(JSON.parse(planJson));
509 } catch (e) {
514 content: META_PROMPTS.ROUTE_PLANNER_RETRY.replace("{{goal}}", goal),
515 }];
516 const retryPlanJson = await callOpenAI(c, retryMessages, { type: "json_object" });
517 return c.json(JSON.parse(retryPlanJson));
518 } catch (retryError) {
535 content: META_PROMPTS.STOP_ARCHITECT.replace("{{task}}", task).replace("{{context}}", contextString),
536 }];
537 const specJson = await callOpenAI(c, architectMessages, { type: "json_object" });
538 const spec = JSON.parse(specJson);
539
541 if (spec.inputs && spec.inputs.length > 0) {
542 const uiBuilderMessages = [{ role: "system", content: META_PROMPTS.UI_BUILDER.replace("{{spec}}", specJson) }];
543 uiJson = await callOpenAI(c, uiBuilderMessages, { type: "json_object" });
544 }
545
561 const contextString = JSON.stringify(context, null, 2);
562
563 const specJson = await callOpenAI(c, [{
564 role: "system",
565 content: META_PROMPTS.STOP_ARCHITECT.replace("{{task}}", task).replace("{{context}}", contextString),
566 }], { type: "json_object" });
567 let executionPrompt = await callOpenAI(c, [{
568 role: "system",
569 content: META_PROMPTS.LOGIC_WEAVER.replace("{{spec}}", specJson).replace("{{context}}", contextString),
574 }
575
576 const openai = new OpenAI();
577 const aiStream = await openai.chat.completions.create({
578 model: "gpt-4o",
579 messages: [{ role: "user", content: executionPrompt }],
2import { Hono } from "npm:hono@4.4.12";
3// @ts-ignore
4import { OpenAI } from "https://esm.town/v/std/openai?v=4";
5// @ts-ignore
6import { blob } from "https://esm.town/v/std/blob";
91
92// --- HELPER FUNCTIONS ---
93async function callOpenAI(messages, response_format = { type: "text" }) {
94 const openai = new OpenAI();
95 const completion = await openai.chat.completions.create({
96 model: "gpt-4o",
97 messages: messages,
171 try {
172 const planMessages = [{ role: "system", content: META_PROMPTS.WORKFLOW_PLANNER.replace("{{goal}}", input) }];
173 const planJson = await callOpenAI(planMessages, { type: "json_object" });
174 let plan = JSON.parse(planJson);
175
185 for (const step of plan.steps) {
186 const contextString = JSON.stringify(stepContext, null, 2);
187 const specJson = await callOpenAI(
188 [{
189 role: "system",
192 { type: "json_object" },
193 );
194 const executionPrompt = await callOpenAI([{
195 role: "system",
196 content: META_PROMPTS.LOGIC_WEAVER.replace("{{spec}}", specJson).replace("{{context}}", contextString),
197 }]);
198 const stepOutput = await callOpenAI([{ role: "user", content: executionPrompt }]);
199 const outputKey = `step_${step.id}_output`;
200 stepContext[outputKey] = stepOutput;
223 try {
224 const messages = [{ role: "system", content: META_PROMPTS.WORKFLOW_PLANNER.replace("{{goal}}", input) }];
225 const planJson = await callOpenAI(messages, { type: "json_object" });
226 let plan = JSON.parse(planJson);
227 if (Array.isArray(plan)) {
289 try {
290 const contextString = JSON.stringify(taskData.metadata.context, null, 2);
291 const specJson = await callOpenAI(
292 [{
293 role: "system",
299 { type: "json_object" },
300 );
301 let executionPrompt = await callOpenAI([{
302 role: "system",
303 content: META_PROMPTS.LOGIC_WEAVER.replace("{{spec}}", specJson).replace("{{context}}", contextString),
304 }]);
305 const stepOutput = await callOpenAI([{ role: "user", content: executionPrompt }]);
306
307 // Create a new compliant artifact for the step output
2import { Hono } from "npm:hono@4.4.12";
3// @ts-ignore
4import { OpenAI } from "https://esm.town/v/std/openai?v=4";
5
6// --- Emergent: The JIT Workflow Engine v3.0 (Autonomous Task Architect) ---
329const app = new Hono();
330
331async function callOpenAI(c, messages, response_format = { type: "text" }) {
332 const openai = new OpenAI();
333 const completion = await openai.chat.completions.create({
334 model: "gpt-4o",
335 messages: messages,
338
339 if (!completion || !completion.choices || completion.choices.length === 0) {
340 console.error("Invalid response from OpenAI API:", JSON.stringify(completion, null, 2));
341 throw new Error("The AI service returned an invalid or empty response.");
342 }
349 try {
350 const messages = [{ role: "system", content: META_PROMPTS.WORKFLOW_PLANNER.replace("{{goal}}", goal) }];
351 const planJson = await callOpenAI(c, messages, { type: "json_object" });
352 return c.json(JSON.parse(planJson));
353 } catch (e) {
358 content: META_PROMPTS.WORKFLOW_PLANNER_RETRY.replace("{{goal}}", goal),
359 }];
360 const retryPlanJson = await callOpenAI(c, retryMessages, { type: "json_object" });
361 return c.json(JSON.parse(retryPlanJson));
362 } catch (retryError) {
380 content: META_PROMPTS.TASK_ARCHITECT.replace("{{task}}", task).replace("{{context}}", contextString),
381 }];
382 const specJson = await callOpenAI(c, architectMessages, { type: "json_object" });
383 const spec = JSON.parse(specJson);
384
387 if (spec.inputs && spec.inputs.length > 0) {
388 const uiBuilderMessages = [{ role: "system", content: META_PROMPTS.UI_BUILDER.replace("{{spec}}", specJson) }];
389 uiJson = await callOpenAI(c, uiBuilderMessages, { type: "json_object" });
390 }
391
407 const contextString = JSON.stringify(context, null, 2);
408
409 const specJson = await callOpenAI(c, [{
410 role: "system",
411 content: META_PROMPTS.TASK_ARCHITECT.replace("{{task}}", task).replace("{{context}}", contextString),
412 }], { type: "json_object" });
413 let executionPrompt = await callOpenAI(c, [{
414 role: "system",
415 content: META_PROMPTS.LOGIC_WEAVER.replace("{{spec}}", specJson).replace("{{context}}", contextString),
420 }
421
422 const openai = new OpenAI();
423 const aiStream = await openai.chat.completions.create({
424 model: "gpt-4o",
425 messages: [{ role: "user", content: executionPrompt }],
77- **Blob storage**: `import { blob } from "https://esm.town/v/std/blob"`
78- **SQLite**: `import { sqlite } from "https://esm.town/v/stevekrouse/sqlite"`
79- **OpenAI**: `import { OpenAI } from "https://esm.town/v/std/openai"`
80- **Email**: `import { email } from "https://esm.town/v/std/email"`
81
77- **Blob storage**: `import { blob } from "https://esm.town/v/std/blob"`
78- **SQLite**: `import { sqlite } from "https://esm.town/v/stevekrouse/sqlite"`
79- **OpenAI**: `import { OpenAI } from "https://esm.town/v/std/openai"`
80- **Email**: `import { email } from "https://esm.town/v/std/email"`
81
2import { Hono } from "npm:hono@4.4.12";
3// @ts-ignore
4import { OpenAI } from "https://esm.town/v/std/openai?v=4";
5// --- Banks 7/22/2025. For Leo & Niko. Bugfix for API calls. Final version. <3
6// --- josh@dereticular.com
643 const userInput = `Occupation: ${occupation.title}, Task: ${task.task}`;
644 try {
645 const openai = new OpenAI();
646 const completion = await openai.chat.completions.create({
647 model: "gpt-4o",
648 messages: [{ role: "system", content: PROMPT_REFINER_SYSTEM_PROMPT }, { role: "user", content: userInput }],
659 if (!refined_prompt) return c.json({ error: "refined_prompt is required" }, 400);
660 try {
661 const openai = new OpenAI();
662 const completion = await openai.chat.completions.create({
663 model: "gpt-4o",
664 messages: [{ role: "system", content: INPUT_EXTRACTOR_SYSTEM_PROMPT }, { role: "user", content: refined_prompt }],
681
682 try {
683 const openai = new OpenAI();
684 const taskCompletion = await openai.chat.completions.create({
685 model: "gpt-4o",
686 messages: [{ role: "user", content: finalUserPrompt }],
689 if (!taskOutput) throw new Error("The AI returned no content.");
690
691 const htmlCompletion = await openai.chat.completions.create({
692 model: "gpt-4o",
693 messages: [{ role: "system", content: HTML_FORMATTER_SYSTEM_PROMPT }, { role: "user", content: taskOutput }],
2import { Hono } from "npm:hono@4.4.12";
3// @ts-ignore
4import { OpenAI } from "https://esm.town/v/std/openai?v=4";
5// --- Banks 7/22/2025. Dental Practice AI Task Automator.
6// --- josh@dereticular.com
992 const userInput = `Occupation: ${occupation.title}, Task: ${task.task}`;
993 try {
994 const openai = new OpenAI();
995 const completion = await openai.chat.completions.create({
996 model: "gpt-4o",
997 messages: [{ role: "system", content: PROMPT_REFINER_SYSTEM_PROMPT }, { role: "user", content: userInput }],
1013 const userInput = `Occupation: ${occupation_title}, Task: ${task}`;
1014 try {
1015 const openai = new OpenAI();
1016 const completion = await openai.chat.completions.create({
1017 model: "gpt-4o",
1018 messages: [{ role: "system", content: PROMPT_REFINER_SYSTEM_PROMPT }, { role: "user", content: userInput }],
1031 if (!refined_prompt) return c.json({ error: "refined_prompt is required" }, 400);
1032 try {
1033 const openai = new OpenAI();
1034 const completion = await openai.chat.completions.create({
1035 model: "gpt-4o",
1036 messages: [{ role: "system", content: INPUT_EXTRACTOR_SYSTEM_PROMPT }, { role: "user", content: refined_prompt }],
1050 const userInput = JSON.stringify({ type: "occupations", industry: industry });
1051 try {
1052 const openai = new OpenAI();
1053 const completion = await openai.chat.completions.create({
1054 model: "gpt-4o",
1055 messages: [
1072 const userInput = JSON.stringify({ type: "tasks", occupation: occupation_name });
1073 try {
1074 const openai = new OpenAI();
1075 const completion = await openai.chat.completions.create({
1076 model: "gpt-4o",
1077 messages: [
1101
1102 try {
1103 const openai = new OpenAI();
1104 const agentCompletion = await openai.chat.completions.create({
1105 model: "gpt-4o",
1106 messages: [
1116 throw new Error("The agent returned no content.");
1117 }
1118 const htmlCompletion = await openai.chat.completions.create({
1119 model: "gpt-4o",
1120 messages: [
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },
8 id: string;
9 name: string;
10 type: 'yahoo_api' | 'openai' | 'processing';
11 status: 'pending' | 'loading' | 'success' | 'error';
12 startTime?: string;
411 stepType = 'yahoo_api';
412 break;
413 case 'openai_request':
414 stepId = 'openai_request';
415 stepName = 'OpenAI Analysis Request';
416 stepType = 'openai';
417 break;
418 case 'openai_response':
419 stepId = 'openai_response';
420 stepName = 'OpenAI Analysis Response';
421 stepType = 'openai';
422 break;
423 case 'performance':