Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set locale and timezone using the react-native-localize library #373

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/example_rn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"react-native": "0.73.2",
"react-native-device-info": "^10.12.0",
"react-native-navigation": "^7.37.2",
"react-native-localize": "^3.0.0",
"posthog-react-native-session-replay": "^0.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion posthog-ai/src/anthropic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class WrappedMessages extends AnthropicOriginal.Messages {
return parentPromise.then((value) => {
const passThroughStream = new PassThrough({ objectMode: true })
let accumulatedContent = ''
let usage: { inputTokens: number; outputTokens: number } = {
const usage: { inputTokens: number; outputTokens: number } = {
Copy link
Contributor

@k11kirky k11kirky Feb 5, 2025

Choose a reason for hiding this comment

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

this should have stayed let - its gets reasigned under

Copy link
Member Author

Choose a reason for hiding this comment

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

the linter did it automatically btw (eslint, prettier), but as I can see it, the usage props are resigned, but not the usage itself.
Happy to change it back but am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

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

the 2 openai usage gets reasigned, but not the anthropic one.

inputTokens: 0,
outputTokens: 0,
}
Expand Down
4 changes: 2 additions & 2 deletions posthog-ai/src/vercel/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const createInstrumentationMiddleware = (
const middleware: LanguageModelV1Middleware = {
wrapGenerate: async ({ doGenerate, params }) => {
const startTime = Date.now()
let mergedParams = {
const mergedParams = {
...options,
...mapVercelParams(params),
}
Expand Down Expand Up @@ -143,7 +143,7 @@ export const createInstrumentationMiddleware = (
const startTime = Date.now()
let generatedText = ''
let usage: { inputTokens?: number; outputTokens?: number } = {}
let mergedParams = {
const mergedParams = {
...options,
...mapVercelParams(params),
}
Expand Down
2 changes: 2 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Next

1. chore: set locale and timezone using the react-native-localize library

# 3.6.4 - 2025-02-03

1. fix: improve session replay linked flag type handling
Expand Down
1 change: 1 addition & 0 deletions posthog-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"react-native": "^0.69.1",
"react-native-device-info": "^10.3.0",
"react-native-navigation": "^6.0.0",
"react-native-localize": "^3.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: react-native-localize is added as a devDependency but not as a peerDependency. Since it's replacing core functionality, it should probably be added to peerDependencies with an appropriate version range.

"posthog-react-native-session-replay": "^0.1"
},
"peerDependencies": {
Expand Down
22 changes: 22 additions & 0 deletions posthog-react-native/src/native-deps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { OptionalExpoFileSystem } from './optional/OptionalExpoFileSystem'
import { OptionalExpoLocalization } from './optional/OptionalExpoLocalization'
import { OptionalReactNativeDeviceInfo } from './optional/OptionalReactNativeDeviceInfo'
import { PostHogCustomAppProperties, PostHogCustomStorage } from './types'
import { OptionalReactNativeLocalize } from './optional/OptionalReactNativeLocalize'

export const getAppProperties = (): PostHogCustomAppProperties => {
let deviceType = 'Mobile'
Expand Down Expand Up @@ -58,6 +59,27 @@ export const getAppProperties = (): PostHogCustomAppProperties => {
if (OptionalExpoLocalization) {
properties.$locale = OptionalExpoLocalization.locale
properties.$timezone = OptionalExpoLocalization.timezone
} else if (OptionalReactNativeLocalize) {
const localesFn = OptionalReactNativeLocalize.getLocales
if (localesFn) {
const locales = localesFn()

if (locales && locales.length > 0) {
const languageTag = locales[0].languageTag
if (languageTag) {
properties.$locale = languageTag
}
}
}
Comment on lines +63 to +73
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider adding error handling for localesFn() call which could throw an exception


const timezoneFn = OptionalReactNativeLocalize.getTimeZone
if (timezoneFn) {
const timezone = timezoneFn()

if (timezone) {
properties.$timezone = timezone
}
}
Comment on lines +75 to +82
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider adding error handling for timezoneFn() call which could throw an exception

}

return properties
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface ReactNativeLocalize {
getLocales: () => {
languageCode: string
scriptCode?: string
countryCode: string
languageTag: string
isRTL: boolean
}[]

getTimeZone(): string
}

import type ReactNativeLocalize from 'react-native-localize'

export let OptionalReactNativeLocalize: typeof ReactNativeLocalize | undefined = undefined

// web support requires webpack
// https://github.com/zoontek/react-native-localize#web-support
try {
OptionalReactNativeLocalize = require('react-native-localize')
} catch (e) {}
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Empty catch block could silently fail. Consider logging the error when in debug mode.

84 changes: 51 additions & 33 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@
resolved "https://registry.yarnpkg.com/@0no-co/graphql.web/-/graphql.web-1.0.9.tgz#b0d053a5b892ea3e2f42a4ea2a92315b517d65d3"
integrity sha512-lXSg4bDHvP8CiMdpQf9f/rca12IIjXHN/p0Rc5mgzgLe4JBlIoA1zFa9NKhfG1bW0OyI2hgaOldFCfkEQwZuEQ==

"@ai-sdk/provider-utils@2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@ai-sdk/provider-utils/-/provider-utils-2.1.0.tgz#40f060252a192a31063300dd277a04df0eba604e"
integrity sha512-rBUabNoyB25PBUjaiMSk86fHNSCqTngNZVvXxv8+6mvw47JX5OexW+ZHRsEw8XKTE8+hqvNFVzctaOrRZ2i9Zw==
"@ai-sdk/provider-utils@2.1.6":
version "2.1.6"
resolved "https://registry.yarnpkg.com/@ai-sdk/provider-utils/-/provider-utils-2.1.6.tgz#5b2bff2d44ffa8ec629109ea2b28d2a7528104b4"
integrity sha512-Pfyaj0QZS22qyVn5Iz7IXcJ8nKIKlu2MeSAdKJzTwkAks7zdLaKVB+396Rqcp1bfQnxl7vaduQVMQiXUrgK8Gw==
dependencies:
"@ai-sdk/provider" "1.0.4"
"@ai-sdk/provider" "1.0.7"
eventsource-parser "^3.0.0"
nanoid "^3.3.8"
secure-json-parse "^2.7.0"

"@ai-sdk/provider@1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@ai-sdk/provider/-/provider-1.0.4.tgz#fe041f09e053235922ab9706be29fedf63c53bd0"
integrity sha512-lJi5zwDosvvZER3e/pB8lj1MN3o3S7zJliQq56BRr4e9V3fcRyFtwP0JRxaRS5vHYX3OJ154VezVoQNrk0eaKw==
"@ai-sdk/provider@1.0.7":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@ai-sdk/provider/-/provider-1.0.7.tgz#66e07407678153b514aebf3f1c3c035e5e833380"
integrity sha512-q1PJEZ0qD9rVR+8JFEd01/QM++csMT5UVwYXSN2u54BrVw/D8TZLTeg2FEfKK00DgAx0UtWd8XOhhwITP9BT5g==
dependencies:
json-schema "^0.4.0"

"@ai-sdk/react@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@ai-sdk/react/-/react-1.1.0.tgz#7059fb3981818667856d3f9afff312e429c0d6b8"
integrity sha512-U5lBbLyf1pw79xsk5dgHSkBv9Jta3xzWlOLpxsmHlxh1X94QOH3e1gm+nioQ/JvTuHLm23j2tz3i4MpMdchwXQ==
"@ai-sdk/react@1.1.9":
version "1.1.9"
resolved "https://registry.yarnpkg.com/@ai-sdk/react/-/react-1.1.9.tgz#9ec1f2637071a8e65108d10c4774e2cd31f1f013"
integrity sha512-2si293+NYs3WbPfHXSZ4/71NtYV0zxYhhHSL4H1EPyHU9Gf/H81rhjsslvt45mguPecPkMG19/VIXDjJ4uTwsw==
dependencies:
"@ai-sdk/provider-utils" "2.1.0"
"@ai-sdk/ui-utils" "1.1.0"
"@ai-sdk/provider-utils" "2.1.6"
"@ai-sdk/ui-utils" "1.1.9"
swr "^2.2.5"
throttleit "2.1.0"

"@ai-sdk/ui-utils@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@ai-sdk/ui-utils/-/ui-utils-1.1.0.tgz#4c31029bae0d06fd2c2530dd214eff434c5a3787"
integrity sha512-ETXwdHaHwzC7NIehbthDFGwsTFk+gNtRL/lm85nR4WDFvvYQptoM/7wTANs0p0H7zumB3Ep5hKzv0Encu8vSRw==
"@ai-sdk/ui-utils@1.1.9":
version "1.1.9"
resolved "https://registry.yarnpkg.com/@ai-sdk/ui-utils/-/ui-utils-1.1.9.tgz#37d9125c359c5de85026a9c5cb17b42e81d9dcd6"
integrity sha512-o0tDopdtHqgr9FAx0qSkdwPUDSdX+4l42YOn70zvs6+O+PILeTpf2YYV5Xr32TbNfSUq1DWLLhU1O7/3Dsxm1Q==
dependencies:
"@ai-sdk/provider" "1.0.4"
"@ai-sdk/provider-utils" "2.1.0"
"@ai-sdk/provider" "1.0.7"
"@ai-sdk/provider-utils" "2.1.6"
zod-to-json-schema "^3.24.1"

"@ampproject/remapping@^2.2.0":
Expand All @@ -51,6 +51,19 @@
"@jridgewell/gen-mapping" "^0.3.5"
"@jridgewell/trace-mapping" "^0.3.24"

"@anthropic-ai/sdk@^0.36.3":
version "0.36.3"
resolved "https://registry.yarnpkg.com/@anthropic-ai/sdk/-/sdk-0.36.3.tgz#0ca754f047e2847c8b1d064550f05e9a8e7e16b3"
integrity sha512-+c0mMLxL/17yFZ4P5+U6bTWiCSFZUKJddrv01ud2aFBWnTPLdRncYV76D3q1tqfnL7aCnhRtykFnoCFzvr4U3Q==
dependencies:
"@types/node" "^18.11.18"
"@types/node-fetch" "^2.6.4"
abort-controller "^3.0.0"
agentkeepalive "^4.2.1"
form-data-encoder "1.7.2"
formdata-node "^4.3.2"
node-fetch "^2.6.7"

"@babel/cli@^7.19.3":
version "7.25.9"
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.25.9.tgz#51036166fd0e9cfb26eee1b9ddc264a0d6d5f843"
Expand Down Expand Up @@ -2927,15 +2940,15 @@ aggregate-error@^3.0.0:
clean-stack "^2.0.0"
indent-string "^4.0.0"

ai@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/ai/-/ai-4.1.0.tgz#3150a91da72fb9dd7df25a781554f9a77114ca4a"
integrity sha512-95nI9hBSSAKPrMnpJbaB3yqvh+G8BS4/EtFz3HR0HgEDJpxC0R6JAlB8+B/BXHd/roNGBrS08Z3Zain/6OFSYA==
ai@^4.0.0:
version "4.1.19"
resolved "https://registry.yarnpkg.com/ai/-/ai-4.1.19.tgz#bd67eb2d502c6df5c20f7fe4d18f3c8d2d9a564e"
integrity sha512-Xx498vbFVN4Y3F4kWF59ojLyn/d++NbSZwENq1zuSFW4OjwzTf79jtMxD+BYeMiDH+mgIrmROY/ONtqMOchZGw==
dependencies:
"@ai-sdk/provider" "1.0.4"
"@ai-sdk/provider-utils" "2.1.0"
"@ai-sdk/react" "1.1.0"
"@ai-sdk/ui-utils" "1.1.0"
"@ai-sdk/provider" "1.0.7"
"@ai-sdk/provider-utils" "2.1.6"
"@ai-sdk/react" "1.1.9"
"@ai-sdk/ui-utils" "1.1.9"
"@opentelemetry/api" "1.9.0"
jsondiffpatch "0.6.0"

Expand Down Expand Up @@ -8447,10 +8460,10 @@ open@^8.0.4, open@^8.3.0:
is-docker "^2.1.1"
is-wsl "^2.2.0"

openai@^4.79.1:
version "4.79.1"
resolved "https://registry.yarnpkg.com/openai/-/openai-4.79.1.tgz#2976f4d20d577cb5f9b7c6664de08745348d5461"
integrity sha512-M7P5/PKnT/S/B5v0D64giC9mjyxFYkqlCuQFzR5hkdzMdqUuHf8T1gHhPGPF5oAvu4+PO3TvJv/qhZoS2bqAkw==
openai@^4.0.0:
version "4.82.0"
resolved "https://registry.yarnpkg.com/openai/-/openai-4.82.0.tgz#9a28ba6552f94639c2de4278255ad669940b6267"
integrity sha512-1bTxOVGZuVGsKKUWbh3BEwX1QxIXUftJv+9COhhGGVDTFwiaOd4gWsMynF2ewj1mg6by3/O+U8+EEHpWRdPaJg==
dependencies:
"@types/node" "^18.11.18"
"@types/node-fetch" "^2.6.4"
Expand Down Expand Up @@ -8984,6 +8997,11 @@ react-native-gradle-plugin@^0.0.7:
resolved "https://registry.yarnpkg.com/react-native-gradle-plugin/-/react-native-gradle-plugin-0.0.7.tgz#96602f909745239deab7b589443f14fce5da2056"
integrity sha512-+4JpbIx42zGTONhBTIXSyfyHICHC29VTvhkkoUOJAh/XHPEixpuBduYgf6Y4y9wsN1ARlQhBBoptTvXvAFQf5g==

react-native-localize@^3.0.0:
version "3.4.1"
resolved "https://registry.yarnpkg.com/react-native-localize/-/react-native-localize-3.4.1.tgz#df07c13231b4e8cc949e68513b124105f861229c"
integrity sha512-NJqJGBUpHtD/MpLCCkrNiqNZ+xFwbHCivxaoN1Oeb8tBAiZr/IqgP3E+MgnqmmdTMOJ33llUfiW3EM6pEIb33w==

react-native-navigation@^6.0.0:
version "6.12.2"
resolved "https://registry.yarnpkg.com/react-native-navigation/-/react-native-navigation-6.12.2.tgz#1389534e868fe4774a1e567b232d59496d8c4c6f"
Expand Down