Skip to content

Commit

Permalink
Add the logger (#213)
Browse files Browse the repository at this point in the history
This was a painful oversight that made observability impossible for
`toTextStream`.
  • Loading branch information
NickHeiner committed Jul 25, 2023
1 parent 68adddd commit 5017e6f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/ai-jsx/package.json
Expand Up @@ -4,7 +4,7 @@
"repository": "fixie-ai/ai-jsx",
"bugs": "https://github.com/fixie-ai/ai-jsx/issues",
"homepage": "https://ai-jsx.com",
"version": "0.5.15",
"version": "0.5.16",
"volta": {
"extends": "../../package.json"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/ai-jsx/src/stream/index.ts
@@ -1,3 +1,4 @@
import { LogImplementation } from '../core/log.js';
import {
isElement as isAIElement,
Element,
Expand Down Expand Up @@ -178,9 +179,9 @@ export function toSerializedStreamResponse(
* this allows the response to be easily consumed by other frameworks (such as https://sdk.vercel.ai/)
* but does not support UI components or concurrently streaming multiple parts of the tree.
*/
export function toTextStream(renderable: Renderable): ReadableStream<Uint8Array> {
export function toTextStream(renderable: Renderable, logger?: LogImplementation): ReadableStream<Uint8Array> {
let previousValue = '';
const generator = createRenderContext().render(renderable, { appendOnly: true })[Symbol.asyncIterator]();
const generator = createRenderContext({ logger }).render(renderable, { appendOnly: true })[Symbol.asyncIterator]();
return new ReadableStream({
async pull(controller) {
const next = await generator.next();
Expand Down
6 changes: 5 additions & 1 deletion packages/docs/docs/changelog.md
@@ -1,6 +1,10 @@
# Changelog

## 0.5.15
## 0.5.16

- Update `toTextStream` to accept a `logger`, so you can now see log output when you're running AI.JSX on the server and outputting to a stream. See [AI + UI](./guides/ai-ui.md) and [Observability](./guides/observability.md).

## [0.5.15](https://github.com/fixie-ai/ai-jsx/commit/68adddd)

- Add [`MdxChatCompletion`](./guides/mdx.md), so your model calls can now output [MDX](https://mdxjs.com/) using your components.

Expand Down
15 changes: 14 additions & 1 deletion packages/examples/src/fastify.tsx
Expand Up @@ -4,6 +4,8 @@ import * as AI from 'ai-jsx';
import Fastify from 'fastify';
import { toTextStream } from 'ai-jsx/stream';
import { ReadableStream } from 'stream/web';
import { pino } from 'pino';
import { PinoLogger } from 'ai-jsx/core/log';

/**
* To run this demo:
Expand All @@ -20,6 +22,17 @@ const fastify = Fastify({
logger: true,
});

const pinoStdoutLogger = pino({
name: 'ai-jsx',
level: process.env.loglevel ?? 'trace',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
});

function FantasyCharacter() {
return (
<JsonChatCompletion>
Expand All @@ -44,7 +57,7 @@ fastify.get('/stream-sample', async (request, reply) => {
</ChatCompletion>
);
}
const responseStream = toTextStream(<DescribeCharacter />);
const responseStream = toTextStream(<DescribeCharacter />, new PinoLogger(pinoStdoutLogger));
await sendReadableStreamToFastifyReply(reply, responseStream);
});

Expand Down

3 comments on commit 5017e6f

@vercel
Copy link

@vercel vercel bot commented on 5017e6f Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-docs – ./packages/docs

ai-jsx-docs-fixie-ai.vercel.app
ai-jsx-docs.vercel.app
ai-jsx-docs-git-main-fixie-ai.vercel.app
docs.ai-jsx.com

@vercel
Copy link

@vercel vercel bot commented on 5017e6f Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-nextjs-demo – ./packages/nextjs-demo

ai-jsx-nextjs-demo-fixie-ai.vercel.app
ai-jsx-nextjs-demo-git-main-fixie-ai.vercel.app
ai-jsx-nextjs-demo.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 5017e6f Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-tutorial-nextjs – ./packages/tutorial-nextjs

ai-jsx-tutorial-nextjs-git-main-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs.vercel.app
ai-jsx-tutorial-nextjs-fixie-ai.vercel.app

Please sign in to comment.